SQL Server IF…ELSE Condition Statement: T-SQL Select Example

⚡ Smart Summary

IF…ELSE is a control-of-flow statement in SQL Server that runs one block of T-SQL when a Boolean condition evaluates to true, and an optional alternative block when the same condition evaluates to false.

  • 🧭 Conditional logic: Conditional statements let SQL Server run different T-SQL actions depending on whether a condition is true or false.
  • IF and ELSE branches: When the Boolean condition is true the IF block runs; when it is false the optional ELSE block runs instead.
  • 🔢 Boolean expression: The condition must be a Boolean expression that evaluates to true or false, such as (1=1) or a variable comparison.
  • 🧱 BEGIN…END blocks: A branch that runs more than one statement must wrap those statements between the BEGIN and END keywords.
  • ELSE is optional: An IF statement can stand alone without an ELSE part, so a false condition simply produces no action.
  • 🔁 Nested conditions: SQL Server has no ELSE IF keyword, so you nest one IF…ELSE inside another to test multiple thresholds.

SQL Server IF…ELSE Condition Statement with T-SQL SELECT Query Examples

Why do you need Conditional Statements?

Conditional statements in SQL Server help you define different logic and actions for different conditions. They let you perform different actions based on the conditions defined within the statement. In real life, you perform many actions that depend on the outcome of some other activity or situation.

Some real-time examples of a conditional statement are:

  • If it rains tomorrow, I will plan a road trip.
  • If flight tickets are less than $400 from my city, then I will go on vacation in Europe; otherwise, I will prefer a nearby tourist spot.

Here, you can see that one action, like the road trip above, is conditionally dependent on the outcome of another activity, which is “whether it will rain or not tomorrow.” Similarly, MS SQL Server also provides the capability to execute a T-SQL statement conditionally.

IF…ELSE Statement in SQL Server

In MS SQL, IF…ELSE is a type of conditional statement. Any T-SQL statement can be executed conditionally using IF…ELSE. The figure below explains how the IF and ELSE branches work in SQL Server:

Flowchart of SQL Server IF…ELSE showing the true IF branch and the false ELSE branch

  • If the condition evaluates to True, then the T-SQL statements that follow the IF condition in SQL Server are executed.
  • If the condition evaluates to False, then the T-SQL statements that follow the ELSE keyword are executed.
  • Once either the IF T-SQL statements or the ELSE T-SQL statement is executed, the other unconditional T-SQL statements continue execution.

IF…ELSE Syntax and Rules in SQL

Syntax:

IF <Condition>
     {Statement | Block_of_statement}   
[ ELSE   
     {Statement | Block_of_statement}]

Rules:

  • The condition should be a Boolean expression, i.e., the condition results in a Boolean value when it is evaluated.
  • An IF…ELSE statement in SQL can conditionally handle a single T-SQL statement or a block of T-SQL statements.
  • A block of statements should start with the keyword BEGIN and close with the keyword END.
  • Using BEGIN and END helps SQL Server identify the statement block that needs to be executed and separate it from the rest of the T-SQL statements that are not part of the IF…ELSE T-SQL block.
  • ELSE is optional.

IF…ELSE with Only a Numeric Value in Boolean Expression

In this first example, the Boolean expression uses only numeric values. Consider the condition below, which is always true because 1 equals 1.

Condition: TRUE

IF (1=1)
PRINT 'IF STATEMENT: CONDITION IS TRUE'
ELSE
PRINT 'ELSE STATEMENT: CONDITION IS FALSE'

Running the query with the true condition (1=1) prints the IF branch message:

Result of the true numeric IF condition printing the IF STATEMENT message

Condition: FALSE

IF (1=2)
PRINT 'IF STATEMENT: CONDITION IS TRUE'
ELSE
PRINT 'ELSE STATEMENT: CONDITION IS FALSE'

With the false condition (1=2), the ELSE branch runs instead:

Result of the false numeric IF condition printing the ELSE STATEMENT message

Assume that you have a table named ‘Guru99’ with two columns and four rows, as displayed below. We will use this ‘Guru99’ table in the examples that follow.

Sample Guru99 table containing a Tutorial_ID column and four rows of data

IF…ELSE with a Variable in Boolean Expression

Instead of constant numbers, the Boolean expression can compare a variable. The following example declares an integer variable and tests its value.

Condition: TRUE

DECLARE @Course_ID INT = 4

IF (@Course_ID = 4)
Select * from Guru99 where Tutorial_ID = 4
ELSE
Select * from Guru99 where Tutorial_ID != 4

Because @Course_ID equals 4, the IF branch runs and returns the row where Tutorial_ID is 4:

Query result returning the Tutorial_ID 4 row when the variable condition is true

Condition: FALSE

DECLARE @Course_ID INT = 4

IF (@Course_ID != 4)
Select * from Guru99 where Tutorial_ID = 4
ELSE
Select * from Guru99 where Tutorial_ID != 4

Here the condition is false, so the ELSE branch runs and returns every row where Tutorial_ID is not 4:

Query result from the ELSE branch returning all rows except Tutorial_ID 4

IF…ELSE with BEGIN…END

When a branch must run more than one statement, wrap the statements in a BEGIN…END block. The example below runs two SELECT statements in each branch.

Condition: TRUE

DECLARE @Course_ID INT = 2

IF (@Course_ID <=2)
	BEGIN
	Select * from Guru99 where Tutorial_ID = 1
	Select * from Guru99 where Tutorial_ID = 2
	END
ELSE
	BEGIN
	Select * from Guru99 where Tutorial_ID = 3
	Select * from Guru99 where Tutorial_ID = 4
	END

Since @Course_ID is less than or equal to 2, the IF block runs both SELECT statements inside BEGIN…END:

Result of the IF block running two SELECT statements for Tutorial_ID 1 and 2

Condition: FALSE

DECLARE @Course_ID INT = 2

IF (@Course_ID >=3)
	BEGIN
	Select * from Guru99 where Tutorial_ID = 1
	Select * from Guru99 where Tutorial_ID = 2
	END
ELSE
	BEGIN
	Select * from Guru99 where Tutorial_ID = 3
	Select * from Guru99 where Tutorial_ID = 4
	END

This time the condition is false, so the ELSE block executes its two SELECT statements:

Result of the ELSE block running two SELECT statements for Tutorial_ID 3 and 4

IF Statement with No ELSE

You can use an IF statement in SQL without an ELSE part, because the ELSE part is optional. For example:

DECLARE @Course_ID INT = 2

IF (@Course_ID <=2)
	Select * from Guru99 where Tutorial_ID = 1

It prints the following single row:

Single-row result printed by an IF statement that has no ELSE part

Executing the false condition gives no output. Consider the following query:

DECLARE @Course_ID INT = 2

IF (@Course_ID <=0)
	Select * from Guru99 where Tutorial_ID = 1

The result is empty, as shown below:

Empty result set produced when the IF condition without ELSE is false

Nested IF…ELSE Statements

Unlike some other programming languages, SQL Server does not provide an ELSE IF statement inside an IF…ELSE condition. Instead, you can nest one IF…ELSE inside another, as demonstrated below:

 DECLARE @age INT;
SET @age = 60;

IF @age < 18
   PRINT 'underage';
ELSE
BEGIN
   IF @age < 50
      PRINT 'You are below 50';
   ELSE
      PRINT 'Senior';
END;

For an @age of 60, the nested IF…ELSE prints the following result:

Result of the nested IF…ELSE printing Senior for an age of 60

  • The code prints ‘underage’ if the value of @age is below 18.
  • If not, the ELSE part is executed. The ELSE part contains a nested IF…ELSE.
  • If the value of @age is below 50, this prints ‘You are below 50’. If none of these conditions is true, the code prints ‘Senior’.

FAQs

IF…ELSE controls the flow of execution, choosing which T-SQL statements or blocks to run. CASE is an expression that returns a single value inside a query, such as in a SELECT or WHERE clause. CASE cannot run separate statements.

IIF() is a shorthand for a CASE expression, not the IF…ELSE control-of-flow statement. It returns one of two values based on a Boolean test and is used inside queries. Unlike IF…ELSE, IIF() cannot execute separate T-SQL statements or blocks.

No. IF…ELSE is a control-of-flow statement, so it cannot appear inside a SELECT list or a WHERE clause. To return conditional values within a query, use a CASE expression or the IIF() function instead.

Write IF EXISTS (SELECT 1 FROM table WHERE condition) followed by the statement to run. EXISTS returns true when the subquery finds at least one matching row, so the IF branch executes only when the data exists.

Yes. IF…ELSE is used inside stored procedures, triggers, functions, and batches to branch logic. Combined with BEGIN…END blocks, it lets a procedure run different SQL statements depending on parameters or query results.

IF…ELSE evaluates its condition once and runs the matching branch a single time. A WHILE loop keeps repeating its statement block as long as the condition stays true. Use IF…ELSE for branching and WHILE for iteration.

Yes. GitHub Copilot can draft IF…ELSE blocks, BEGIN…END wrappers, and nested conditions from a natural-language prompt. Always review the generated Boolean conditions and branch logic before running the script on your database.

AI and machine-learning assistants translate plain-English rules into IF…ELSE or CASE logic, suggest missing branches, and flag conditions that can never be true. The developer reviews each suggestion for correctness before deploying it.

Summarize this post with: