---
description: Why do you need Conditional Statements? In real life, you perform many actions which are dependent on the outcome of some other activity or situation. Some real-time examples are: If it rains tomorrow
title: SQL Server IF…ELSE Condition Statement: T-SQL Select Example
image: https://www.guru99.com/images/sql-server-if-else-condition-statement.png
---

 

[Skip to content](#main) 

**⚡ 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.

[ Read More ](javascript:void%280%29;) 

![SQL Server IF…ELSE Condition Statement with T-SQL SELECT Query Examples](https://www.guru99.com/images/sql-server-if-else-condition-statement.png)

## 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](https://www.guru99.com/ms-sql-server-tutorial.html) 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:

[](https://www.guru99.com/images/1/030819%5F0910%5FSQLServerIF1.png)

* 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:

[](https://www.guru99.com/images/1/030819%5F0910%5FSQLServerIF2.png)

**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:

[](https://www.guru99.com/images/1/030819%5F0910%5FSQLServerIF3.png)

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.

[](https://www.guru99.com/images/1/030819%5F0910%5FSQLServerIF4.png)

## IF…ELSE with a Variable in Boolean Expression

Instead of constant numbers, the Boolean expression can compare a [variable](https://www.guru99.com/sql-server-variable.html). 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:

[](https://www.guru99.com/images/1/030819%5F0910%5FSQLServerIF5.png)

**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:

[](https://www.guru99.com/images/1/030819%5F0910%5FSQLServerIF6.png)

### RELATED ARTICLES

* [MS SQL Server Tutorial ](https://www.guru99.com/ms-sql-server-tutorial.html "MS SQL Server Tutorial")
* [How to Download and Install SQL Server for Windows (FREE) ](https://www.guru99.com/download-install-sql-server.html "How to Download and Install SQL Server for Windows (FREE)")
* [SQL Server FOREIGN KEY: How to Create with Example ](https://www.guru99.com/sql-server-foreign-key.html "SQL Server FOREIGN KEY: How to Create with Example")
* [Top 50 T-SQL Interview Questions and Answers (2026) ](https://www.guru99.com/t-sql-interview-questions.html "Top 50 T-SQL Interview Questions and Answers (2026)")

## 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:

[](https://www.guru99.com/images/1/030819%5F0910%5FSQLServerIF7.png)

**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:

[](https://www.guru99.com/images/1/030819%5F0910%5FSQLServerIF8.png)

## 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:

[](https://www.guru99.com/images/1/030819%5F0910%5FSQLServerIF9.png)

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:

[](https://www.guru99.com/images/1/030819%5F0910%5FSQLServerIF10.png)

## Nested IF…ELSE Statements

Unlike some other [programming languages](https://www.guru99.com/best-programming-language.html), 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:

[](https://www.guru99.com/images/1/030819%5F0910%5FSQLServerIF11.png)

* 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

🔀 What is the difference between IF…ELSE and the CASE expression in SQL Server?

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.

⚡ How does the IIF() function relate to IF…ELSE?

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.

🚫 Can an IF…ELSE statement be used inside a SELECT or WHERE clause?

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.

🔎 How do you combine IF with EXISTS to check for rows first?

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.

🧩 Can IF…ELSE be used in a stored procedure?

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.

🔁 What is the difference between IF…ELSE and a WHILE loop?

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.

🤖 Can GitHub Copilot generate IF…ELSE logic in T-SQL?

Yes. [GitHub Copilot](https://github.com/features/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.

🧠 How does AI help write conditional T-SQL logic?

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:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/sql-server-if-else-condition-statement.png","url":"https://www.guru99.com/images/sql-server-if-else-condition-statement.png","width":"700","height":"250","caption":"SQL Server IF\u2026ELSE Condition Statement","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/sql-server-if-else.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/sql-server","name":"SQL Server"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/sql-server-if-else.html","name":"SQL Server IF\u2026ELSE Condition Statement: T-SQL Select Example"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/sql-server-if-else.html#webpage","url":"https://www.guru99.com/sql-server-if-else.html","name":"SQL Server IF\u2026ELSE Condition Statement: T-SQL Select Example","dateModified":"2026-07-25T11:26:10+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/sql-server-if-else-condition-statement.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/sql-server-if-else.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/fiona","name":"Fiona Brown","description":"I'm Fiona brown, a Full Stack Developer with over a decade of experience, sharing practical guides on robust and scalable application development.","url":"https://www.guru99.com/author/fiona","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/fiona-brown-author.png","url":"https://www.guru99.com/images/fiona-brown-author.png","caption":"Fiona Brown","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"SQL Server","headline":"SQL Server IF\u2026ELSE Condition Statement: T-SQL Select Example","description":"Why do you need Conditional Statements? In real life, you perform many actions which are dependent on the outcome of some other activity or situation. Some real-time examples are: If it rains tomorrow","keywords":"sqlserver, sql","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/fiona","name":"Fiona Brown"},"dateModified":"2026-07-25T11:26:10+05:30","image":{"@id":"https://www.guru99.com/images/sql-server-if-else-condition-statement.png"},"copyrightYear":"2026","name":"SQL Server IF\u2026ELSE Condition Statement: T-SQL Select Example","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is the difference between IF\u2026ELSE and the CASE expression in SQL Server?","acceptedAnswer":{"@type":"Answer","text":"IF\u2026ELSE 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."}},{"@type":"Question","name":"How does the IIF() function relate to IF\u2026ELSE?","acceptedAnswer":{"@type":"Answer","text":"IIF() is a shorthand for a CASE expression, not the IF\u2026ELSE control-of-flow statement. It returns one of two values based on a Boolean test and is used inside queries. Unlike IF\u2026ELSE, IIF() cannot execute separate T-SQL statements or blocks."}},{"@type":"Question","name":"Can an IF\u2026ELSE statement be used inside a SELECT or WHERE clause?","acceptedAnswer":{"@type":"Answer","text":"No. IF\u2026ELSE 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."}},{"@type":"Question","name":"How do you combine IF with EXISTS to check for rows first?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Can IF\u2026ELSE be used in a stored procedure?","acceptedAnswer":{"@type":"Answer","text":"Yes. IF\u2026ELSE is used inside stored procedures, triggers, functions, and batches to branch logic. Combined with BEGIN\u2026END blocks, it lets a procedure run different SQL statements depending on parameters or query results."}},{"@type":"Question","name":"What is the difference between IF\u2026ELSE and a WHILE loop?","acceptedAnswer":{"@type":"Answer","text":"IF\u2026ELSE 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\u2026ELSE for branching and WHILE for iteration."}},{"@type":"Question","name":"Can GitHub Copilot generate IF\u2026ELSE logic in T-SQL?","acceptedAnswer":{"@type":"Answer","text":"Yes. GitHub Copilot can draft IF\u2026ELSE blocks, BEGIN\u2026END 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."}},{"@type":"Question","name":"How does AI help write conditional T-SQL logic?","acceptedAnswer":{"@type":"Answer","text":"AI and machine-learning assistants translate plain-English rules into IF\u2026ELSE or CASE logic, suggest missing branches, and flag conditions that can never be true. The developer reviews each suggestion for correctness before deploying it."}}]}],"@id":"https://www.guru99.com/sql-server-if-else.html#schema-1150126","isPartOf":{"@id":"https://www.guru99.com/sql-server-if-else.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/sql-server-if-else.html#webpage"}}]}
```
