Introduction
SQL Case statement provides functionality similar to the IF-THEN-ELSE statement. A CASE statement evaluates a list of conditions and returns one of the multiple possible result expressions. Sometimes there is a need to fetch or modify the records based on some conditions. In this case, we can use a CASE expression in SQL. Case can be used in any statement or clause allowing valid expression. For example, you can use a CASE statement with SELECT, UPDATE, DELETE, and SET in clauses such as IN, WHERE, ORDER BY, and HAVING.
Simple CASE Expression
This CASE expression is known as a simple CASE expression. The simple CASE expression compares an expression to a set of simple expressions to determine the result. It compares the expression with each expression in each WHEN clause. If the expression within the WHEN clause matches, it returns the expression of the THEN clause.
The following are some essential points of a simple CASE expression.
- Allows only an equality check.
- Evaluates input_expression and then, in the order specified, evaluate input_expression = when_expression for each WHEN clause.
- Returns the result_expression of the first input_expression = when_expression that evaluates to TRUE.
- If no input_expression = when_expression evaluates to TRUE, the SQL Server Database Engine returns the else_result_expression if an ELSE clause is specified or a NULL value if no ELSE clause is specified.
The following is an example of a simple SQL CASE expression.
CASE expression
WHEN value_1 THEN result_1
WHEN value_2 THEN result_2
...
WHEN value_n THEN result_n
ELSE result
END
Searched CASE Expression
The searched CASE Expression evaluates Boolean expressions to determine the result. We can use boolean, logical, and comparison operators in this CASE Expression.
Some important points about Searched CASE Expression.
- Evaluates in the order specified, Boolean_expression for each WHEN clause.
- Returns result_expression of the first Boolean_expression that evaluates to TRUE.
- If no Boolean_expression evaluates to TRUE, the Database Engine returns the else_result_expression if an ELSE clause is specified or a NULL value if no ELSE clause is specified.
Here is an example of searched CASE expression.
CASE
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result
END
Now we will do some exercises on CASE Expressions.
First, we will create a table and insert some values in that table.
CREATE TABLE tblEmployee (
EMP_IID INT NOT NULL,
EMP_NAME VARCHAR(MAX) NOT NULL,
EMP_AGE INT NOT NULL,
EMP_SALARY INT NOT NULL,
EMP_CITY VARCHAR(MAX) NOT NULL,
EMP_GENDER CHAR(1) NOT NULL
)
INSERT INTO tblEmployee
SELECT 1,'PANKAJ',20, 25000, 'ALWAR', '1' UNION ALL
SELECT 2,'RAHUL',19, 22000, 'JAIPUR', '1' UNION ALL
SELECT 3,'PRIYA',21, 28000, 'ALWAR', '0' UNION ALL
SELECT 4,'SANDEEP',20, 23000, 'JAIPUR', '1' UNION ALL
SELECT 5,'SONAL',22, 32000, 'ALWAR', '0' UNION ALL
SELECT 6,'SANJEEV',21, 50000, 'ALWAR', '1' UNION ALL
SELECT 7,'KOMAL',23, 47000, 'JAIPUR', '0'
Now tblEmployee will look at the following.
SELECT * FROM tblEmployee e;

Now we will see some examples.
Example 1. General use case of a CASE expression
CASE expressions are powerful in SQL, allowing us to create conditional statements within a SELECT, UPDATE, INSERT, or DELETE statement. Here are some general use cases for a CASE expression:
- Classifying data: We can use a CASE expression to classify data into different categories based on certain conditions. For example, use a CASE expression to classify products into price ranges (e.g., low, medium, high) based on their price.
- Calculating values: We can use a CASE expression to calculate a value based on different conditions. For example, use a CASE expression to calculate a discount based on the quantity of items purchased.
- Providing default values: We can use a CASE expression to give a default value if no other conditions are met. For example, use a CASE expression to assign a default priority level to tasks if no priority level is specified.
- Conditional updates: We can use a CASE expression in an UPDATE statement to update different columns based on certain conditions. For example, use a CASE expression to update the price of a product based on the quantity of items in stock.
- Conditional inserts: We can use a CASE expression in an INSERT statement to insert different values based on certain conditions. For example, use a CASE expression to insert a default value for a column if no value is specified.
The below example shows a general use of a CASE expression.
Syntax 1
DECLARE @MYCOUNT INT;
SET @MYCOUNT = 3
SELECT
CASE (@MYCOUNT)
WHEN 1 THEN 'ONE'
WHEN 2 THEN 'TWO'
WHEN 3 THEN 'THREE'
ELSE 'WRONG CHOICE'
END AS [MESSAGE]
This SELECT statement will declare a variable called MYCOUNT and initialize it with a value of 3. It will then use a CASE statement in the SELECT clause to display a message based on the value of MYCOUNT.
The CASE statement has four WHEN clauses, each specifying a value and a corresponding message. If MYCOUNT has a value of 1, the CASE statement will return 'ONE'; if MYCOUNT has a value of 2, the CASE statement will return 'TWO'; if MYCOUNT has a value of 3, the CASE statement will return 'THREE'; otherwise, the CASE statement will return 'WRONG CHOICE.'
The SELECT statement will display the message returned by the CASE statement in a MESSAGE column. In this case, the SELECT statement will return a row with a MESSAGE column containing the' THREE' value.
Output

Syntax 2
DECLARE @MYCOUNT INT;
SET @MYCOUNT = 9
SELECT
CASE
WHEN @MYCOUNT < 5 THEN 'VALUE IS LESS THAN 5'
WHEN @MYCOUNT >= 5 AND
@MYCOUNT < 10 THEN 'VALUE IS LESS THAN 10 BUT GREATER THAN 5'
ELSE 'VALUE GREATER THAN 10'
END AS [MESSAGE]
Output

Example 2. CASE in a SELECT Statement
CASE expression in a SELECT statement to create a derived column that contains a value based on certain criteria. Here are two different examples of using a CASE in a SELECT statement. Here's the basic syntax for using a CASE expression in a SELECT statement:
Syntax 1
SELECT
e.EMP_IID,
e.EMP_NAME,
e.EMP_AGE,
e.EMP_SALARY,
e.EMP_CITY,
EMP_GENDER=
(CASE e.EMP_GENDER
WHEN '0' THEN 'FEMALE'
WHEN '1' THEN 'MALE'
ELSE NULL
END
)
FROM tblEmployee e
Syntax 2
SELECT
e.EMP_IID,
e.EMP_NAME,
e.EMP_AGE,
e.EMP_SALARY,
e.EMP_CITY,
EMP_GENDER=
( CASE
WHEN e.EMP_GENDER ='0' THEN 'FEMALE'
WHEN e.EMP_GENDER ='1' THEN 'MALE'
ELSE NULL
END
)
FROM tblEmployee e
This SELECT statement will retrieve all rows from the tblEmployee table and display the values in the EMP_IID, EMP_NAME, EMP_AGE, EMP_SALARY, and EMP_CITY columns. It will also use a CASE statement to display a text value for the EMP_GENDER column based on the value of the EMP_GENDER column.
The CASE statement in the SELECT clause has two WHEN clauses, each specifying a gender value and a corresponding text value. If the EMP_GENDER column has a value of '0', the CASE statement will return 'FEMALE'; if the EMP_GENDER column has a value of '1', the CASE statement will return 'MALE'; otherwise, the CASE statement will return NULL.
The SELECT statement will retrieve all rows from the tblEmployee table and display the values in the EMP_IID, EMP_NAME, EMP_AGE, EMP_SALARY, and EMP_CITY columns as well as the value returned by the CASE statement for the EMP_GENDER column. If the EMP_GENDER column has a value of '0', the EMP_GENDER column will be displayed as 'FEMALE'; if the EMP_GENDER column has a value of '1', the EMP_GENDER column will be displayed as 'MALE'; otherwise, the EMP_GENDER column will be displayed as NULL.
Output

Example 3. CASE in an UPDATE Statement
Case in UPDATE statement allows us to perform different actions based on different conditions. It can be used to replace the complex if-else statement and in the set clause of an update statement.
Here are some examples of using a CASE expression with an UPDATE statement.






Jaipal ReddyPosted Jun 22, 2015, 12:34 AM
nice work.
Santhakumar MunuswamyPosted Jun 19, 2015, 2:34 PM
Thanks for good work...
Nagaraj SPosted Jun 19, 2015, 11:42 AM
Hi Pankaj Kumar, Awesome explanation. Please give me your email id.
Debasis SahaPosted Jun 19, 2015, 11:13 AM
Good One
Sibeesh VenuPosted Jun 19, 2015, 8:40 AM
Good one.
Gopi ChandPosted Jun 19, 2015, 7:47 AM
Good illustrations
Abhishek AroraPosted Jun 19, 2015, 5:10 AM
Nicely Explained..!!
RakeshPosted Jun 19, 2015, 4:10 AM
good one