EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial IF ELSE Statement in SQL
 

IF ELSE Statement in SQL

Priya Pedamkar
Article byPriya Pedamkar

If-else-statement-in-sql

What is IF ELSE Statement in SQL?

If-else is known as a conditional statement. Similarly in SQL, it is known as the conditional SQL statement. if & else control structure used mostly in the procedures & methods. When If the condition used in the SQL in that case execution takes place as shown in the following expression:

 

 

Syntax:

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

IF(condition, true, false)

If condition expression evaluates to true then true case value is returned by the expression otherwise false case value will be returned.

IF…ELSE statement is also known as the control flow statement. It controls the flow of execution. Use of the IF… ELSE statements can skip SQL statements on a certain condition. The condition should be in boolean expression, which means condition expression results must be either true or false. Any T-SQL (Transact-SQL) can be placed in the conditional IF…ELSE statement.

Syntax Applying in IF…ELSE Statement

In the below-given statement IF conditional statement evaluates to true then SQL statement under IF block is executed otherwise ELSE block is executed.

Syntax #1:

IF <conditional_statement>
BEGIN
{ SQL Statement }
END
END IF;

Syntax #2:

IF <conditional_statement>
BEGIN
{ SQL Statement }
END
ELSE
BEGIN
{ SQL Statement }
END
END IF;

Flow Diagram

Below given diagram shows how the if-else statement plays a role in executing the statement based on the expression evaluation:

IF ELSE Statement in SQL Flow Diagram

Above given flow diagram says the following things:

  • If Condition Evaluates to true in that case true case statement 1 block gets executed.
  • If Condition Evaluates to false in that case false case statement 2 block get executed.

After execution of the IF…ELSE statement then other unconditional statement execution remains to continue.

IF…ELSE Statement in SQL

In MySQL, IF…ELSE statement is a conditional statement. In the above-given flow diagram we can see how different statements executed & skipped conditionally:

  1. The condition should be in boolean expression & must be evaluated in true/false.
  2. IF…ELSE conditionally handle the single or multiple T-SQL statements in its block.
  3. IF…ELSE block of statement starts from BEGIN statement & ends with an END statement.
  4. ELSE statement may be used or not, It’s optional.
  5. BEGIN & END statement helps SQL Server to identify the start & end of the SQL statement that needs to be executed. Also, it separates the T-SQL statement conditionally.

Example #1

In this example, we will see how IF…ELSE condition can be used in the SQL statement. Here placement of the IF…ELSE statement will teach us how it can be placed effectively with the SQL.

SQL statement

The above given table having the record on the foreign key `category_id`. In this table, a procedure is created where IF…ELSE statement that will be called by passing an argument as a category_id.

Code:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `__searchItemsByCategoryId`(IN `categoryId` INT(11))
BEGIN
IF (categoryId <= 8) THEN
SELECT * FROM `oms_items` WHERE `category_id` <= categoryId;
ELSE
SELECT * FROM `oms_items` WHERE `category_id` > categoryId LIMIT 10;
END IF;
END$$
DELIMITER ;

Above given SQL will create a procedure to extract the record based on the category_id. When this procedure will be called by the application by passing an input id as a category_id.

Calling of the procedure can be done in the following way:

Code:

CALL __searchItemsByCategoryId(8);

Result of the procedure call is displayed below:

Calling output

Example #2

In the below-given example, A procedure “__searchItemsByName” is created to find the product by the name as entered at the time of the procedure call. Also if the provided name to the procedure is “null” in that case procedure will fetch all the products.

Code:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `__searchItemsByName`(IN `itemName` TEXT)
BEGIN
IF (itemName != '') THEN
SELECT * FROM `oms_items` WHERE `item_description` LIKE itemName;
ELSE
SELECT * FROM `oms_items`;
END IF;
END$$
DELIMITER

Above given query can be executed through the following way

Code:

CALL __searchItemsByName(‘Capacitor’);

In the above-given procedure call, the parameter passed to the procedure will display those records which will have the ‘Capacitor’ in the ‘item_description’ column as shown in the below screenshot.

‘item_description’

Now if the calling of the above procedure is done without the passing of any values, in that case, all the records will be displayed.

IF ELSE Statement in SQL-4

Example #3

In the below-given table, If `is_featured` needs to be changed to inactive state then we can see how IF can work in the SQL statement.

IF ELSE Statement in SQL-5

Code:

UPDATE `oms_items` SET `is_featured` = IF(`is_featured` = 1, 0, 1) WHERE `item_no`= 10;

After the execution of the above query, the status of the `is_featured` column switch to 0. If `if_featured` column value for the corresponding record is 0 then it will change to 1.

Example #4

In the given table, the following query efficiently displaying the category wise count of the items. In this statement IF queries work like as a ternary statement

Code:

SELECT COUNT(IF(category_id = 7,1,NULL)) motor_parts,
COUNT(IF(category_id = 8,1,NULL)) circuit_items,
COUNT(IF(category_id = 9,1,NULL)) fitting_goods,
COUNT(IF(category_id = 10,1,NULL)) board_parts
FROM oms_items;

Output:

Example -6

The above output can be achieved by using the “group_by” method in the SQL statement.

Conclusion

IF…ELSE is from one of the conditions in SQL which reduces query & makes DML operation faster & efficient. If the condition matches then the statement under the scope of IF block will be executed otherwise statement under ELSE block will be executed. Use of IF, IF…ELSE, IF- ELSEIF-ELSE these statements work differently according to the use cases. ELSE-IF & ELSE is optional it may be used or not It’s totally dependent on the situation.

Recommended Articles

This is a guide to IF-ELSE Statement in SQL. Here we discuss Syntax Applying in IF…ELSE Statement with a flow diagram and examples of how different statements executed & skipped condition. You can also go through our other related articles to learn more –

  1. If Else Statement in Python
  2. If-else Statement in C
  3. Break Statement in C
  4. if else Statement in PHP
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW