IF THEN Statement in SQL

An 'IF THEN' statement in SQL is a conditional control flow mechanism that allows you to execute specific codes based on whether a specified condition is met. It's commonly used within procedural SQL languages, PL/SQL or T-SQL, rather than standard SQL. When the 'IF' clause condition is true, the code block following the 'THEN' keyword is executed. If the condition is false, the code block is skipped. It's important to consider that using 'IF THEN' statements can impact the performance of your SQL scripts, especially when dealing with large datasets. This control structure helps implement logic that depends on specific conditions, making your SQL scripts more dynamic and responsive to varying data scenarios.

Obtain Your SQL Certification

SQL Certification CourseExplore Program
Obtain Your SQL Certification

Let us look at the control-of-flow statements briefly:

1. IF THEN: The IF THEN statement executes the part of the code when the condition specified with the IF statement is evaluated as true. It does not provide any course of action if the condition remains false.

2. IF ELSE: If the condition is evaluated as true, the IF ELSE statement executes the sequence of statements inside the IF statement. The statements enclosed inside the ELSE block get executed when the condition is false.

3. WHILE: The WHILE statement executes the code block if the condition is satisfied. It repeats until the condition evaluates as false.

4. CASE: The CASE statement is applicable when we have multiple conditions to check for a program code. It also allows numerous actions based on various conditions.

5. BEGIN…END: This BEGIN…END block of code allows multiple statements to be grouped, which helps execute the control-of-flow statements.

6. GOTO: This statement differs slightly from the IF ELSE statements. It also checks the conditions first. If satisfied, the program control jumps to a code block specified by the GOTO keyword.

7. TRY…CATCH: The TRY keyword contains the part of the code where errors are possible. If it gets an error, the program control shifts to CATCH, where the sequence of statements to rectify the problem is defined.

8. BREAK: BREAK is often used with loops and conditional statements. It allows the program control to exit the containing block or terminate a given loop.

9. CONTINUE: Used often with the loops, the CONTINUE statement allows you to skip the code and move to the next iteration.

IF THEN statement in SQL

The IF THEN statement is part of the control statements in SQL. Simply put, it helps in decision-making and directs program control flow.

The IF statement associates a condition, and then a sequence of statements follows. If the condition is evaluated as accurate, the statements enclosed within the keywords THEN and END IF get executed. If the condition specified is evaluated as false, the statements don't get executed, and the program does nothing.

Syntax of IF THEN statement

PL/SQL (Oracle)

IF condition THEN
   -- Statements to execute if condition is true
END IF;

T-SQL (SQL Server)

IF condition
BEGIN
   -- Statements to execute if condition is true
END

In both cases:

  • ‘condition’ is an expression that is evaluated as true or false.
  • The statements within the THEN or BEGIN block are executed only if the condition is true.
  • You can extend this syntax with ELSE and ELSEIF for additional conditional logic.

Advance Your Career with SQL

SQL Certification CourseExplore Program
Advance Your Career with SQL

Examples of IF THEN Statements

Here are examples of IF THEN statements in both PL/SQL (Oracle) and T-SQL (SQL Server):

PL/SQL (Oracle)

DECLARE
   salary NUMBER := 50000;
   bonus NUMBER;
BEGIN
   IF salary > 40000 THEN
      bonus := 1000;
   END IF;
   
   DBMS_OUTPUT.PUT_LINE('Bonus: ' || bonus);
END;

Explanation

In this example, if the salary exceeds 40,000, the bonus is set to 1,000. The DBMS_OUTPUT.PUT_LINE procedure then prints the bonus.

T-SQL (SQL Server)

DECLARE @salary INT = 50000;
DECLARE @bonus INT;

IF @salary > 40000
BEGIN
    SET @bonus = 1000;
END;

PRINT 'Bonus: ' + CAST(@bonus AS VARCHAR);

Explanation

In this T-SQL example, if the @salary exceeds 40,000, the @bonus is set to 1,000. The PRINT statement then outputs the bonus.

Example with ELSE (Both PL/SQL and T-SQL)

PL/SQL (Oracle)

DECLARE
   salary NUMBER := 35000;
   bonus NUMBER;
BEGIN
   IF salary > 40000 THEN
      bonus := 1000;
   ELSE
      bonus := 500;
   END IF;
   
   DBMS_OUTPUT.PUT_LINE('Bonus: ' || bonus);
END;

T-SQL (SQL Server)

DECLARE @salary INT = 35000;
DECLARE @bonus INT;

IF @salary > 40000
BEGIN
    SET @bonus = 1000;
END
ELSE
BEGIN
    SET @bonus = 500;
END;

PRINT 'Bonus: ' + CAST(@bonus AS VARCHAR);

In these examples, if the salary is greater than 40,000, the bonus is set to 1,000; otherwise, the bonus is set to 500.

Start Your Journey to Data Mastery

SQL Certification CourseExplore Program
Start Your Journey to Data Mastery

Conclusion

The IF THEN statement in SQL is a powerful tool for implementing conditional logic within your database procedures. It allows you to control the flow of your SQL scripts based on specific conditions, making your operations more dynamic and adaptable. Whether you're working in Oracle's PL/SQL or SQL Server's T-SQL, mastering conditional statements like IF THEN is essential for efficient database management and complex query building.

To deepen your understanding of SQL and enhance your database management skills, consider enrolling in the SQL Certification Course by Simplilearn. This comprehensive course covers everything from basic SQL queries to advanced database management techniques, equipping you with the knowledge needed to excel in your career.

FAQs

1. Can I use the IF THEN statement with SELECT queries?

The IF THEN statement in standard SQL is not directly used within SELECT queries. However, you can achieve similar conditional logic within a SELECT statement using functions like CASE or IF (in MySQL).

2. What are some common use cases for the IF THEN statement in SQL?

The IF THEN statement in SQL, particularly in procedural extensions like PL/SQL (Oracle) and T-SQL (SQL Server), is commonly used in various scenarios to control logic flow based on conditions. Here are some common use cases:

  • Data Validation
  • Conditional Data Processing
  • Error Handling
  • Complex Business Logic
  • Conditional Execution of SQL Statements
  • Dynamic Query Building
  • Trigger Actions
  • Conditional Logging

3. Can I nest IF THEN statements in SQL?

Yes, you can nest IF THEN statements in SQL, particularly in procedural SQL languages like PL/SQL (Oracle) and T-SQL (SQL Server). Nesting IF THEN statements allows you to evaluate multiple conditions hierarchically, where the execution of one IF THEN block depends on the outcome of another.

4. Can IF THEN statements be executed in stored procedures?

Yes, IF THEN statements can be executed within stored procedures in SQL, particularly in procedural extensions like PL/SQL (Oracle) and T-SQL (SQL Server). In stored procedures, IF THEN statements are commonly used to control the logic flow, allowing the procedure to perform different actions based on specific conditions.

About the Author

Pulkit JainPulkit Jain

Pulkit Jain is a Product Manager for Salesforce & Payments at Simplilearn, where he drives impactful product launches and updates. With deep expertise in CRM, cloud & DevOps, and product marketing, Pulkit has a proven track record in steering software development and innovation.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.