SQL Keywords

SQL IF

What is the SQL IF statement?

Executes one SQL statement or block only when a Boolean expression evaluates to TRUE, optionally providing an ELSE branch.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL IF: SQL Server (T-SQL) – full support Azure SQL Database – full support MySQL – IF statement in stored programs; IF(expr, true_val, false_val) function at query level MariaDB – same as MySQL Oracle – PL/SQL IF inside blocks, not at top-level SQL PostgreSQL – IF only in PL/pgSQL blocks; top-level uses CASE SQLite – no native IF statement (use CASE)

SQL IF Full Explanation

SQL IF is a control-of-flow statement available in T-SQL and some other dialects that lets you branch logic based on a Boolean expression. When the expression is TRUE, the statement or BEGIN...END block that follows runs; otherwise, the optional ELSE branch runs. IF is evaluated at run-time, so the chosen branch is the only one executed, saving resources compared with running both branches. IF can appear in ad-hoc batches, stored procedures, functions, triggers, and scripts. Nesting IF statements is allowed up to 32 levels in SQL Server. Unlike the CASE expression, IF controls which statements execute rather than returning a value inside a single expression.

SQL IF Syntax

IF <boolean_expression>
    { sql_statement | BEGIN
        statement_block
      END }
[ELSE
    { sql_statement | BEGIN
        statement_block
      END }];

SQL IF Parameters

  • boolean_expression (Boolean) - Any expression that evaluates to TRUE, FALSE, or UNKNOWN.
  • statement_block (SQL) - One or more valid T-SQL statements, wrapped in BEGIN...END when multiple.

Example Queries Using SQL IF

-- Example 1: Simple IF
IF EXISTS (SELECT 1 FROM users WHERE id = 42)
    PRINT 'User exists';
ELSE
    PRINT 'User not found';

-- Example 2: IF inside stored procedure
CREATE OR ALTER PROCEDURE usp_update_status @user_id INT, @status VARCHAR(20)
AS
BEGIN
    IF @status NOT IN ('active','inactive')
    BEGIN
        RAISERROR ('Invalid status', 16, 1);
        RETURN;
    END

    UPDATE users SET status = @status WHERE id = @user_id;
END;
GO

-- Example 3: Nested IF with BEGIN...END
IF (SELECT COUNT(*) FROM orders WHERE total > 1000) > 50
BEGIN
    IF NOT EXISTS (SELECT 1 FROM big_spender_report)
        INSERT INTO big_spender_report(run_date) VALUES (GETDATE());
END;

Expected Output Using SQL IF

  • Only the branch whose condition evaluates to TRUE runs
  • Statements in the other branch are skipped
  • If no ELSE is supplied and the condition is FALSE/UNKNOWN, nothing occurs

Use Cases with SQL IF

  • Skip expensive queries when data is absent.
  • Validate parameters in stored procedures before continuing.
  • Conditionally raise errors or exit early.
  • Execute maintenance tasks only during off-hours.
  • Implement branching logic in database scripts.

Common Mistakes with SQL IF

  • Forgetting BEGIN...END around multiple statements, causing only the first to be conditional.
  • Using a single equal sign (=) instead of the comparison operator (=A) inside the Boolean expression.
  • Assuming IF works inside plain SELECT queries; it is only for procedural/batch contexts.
  • Expecting IF to return a value like CASE; it controls flow, not expressions.

Related Topics

CASE expression, IIF function, BEGIN END, ELSE, WHILE, GOTO, TRY CATCH

First Introduced In

Sybase SQL Server 4.x (early 1990s)

Frequently Asked Questions

What is the difference between IF and CASE?

IF controls which statements run; CASE returns a value within a single statement.

Can IF be nested?

Yes. SQL Server allows up to 32 nested IF levels, each with its own BEGIN...END block.

Does IF work in PostgreSQL?

Only inside PL/pgSQL functions or DO blocks. PostgreSQL queries themselves use CASE for conditional values.

How do I conditionally update a table?

Wrap the UPDATE in an IF block that checks your desired condition, optionally adding an ELSE branch for alternative actions.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!