SQL CASE Statement

The SQL Server Case Statement is similar to the control flow statements (something like IF ELSE). This statement evaluates the series of conditional expressions provided in WHEN and returns the result set.

SQL Server Case Statement Syntax

The syntax of the case is shown below.

CASE Input_Expression
     WHEN test_expression THEN result_expression
     ELSE default_expression

-- Searched Syntax
WHEN Boolean_expression THEN result_expression
     ELSE default_expression

For the SQL Server Searched case expression syntax, Input_Expression is optional so we can remove it. And the arguments are:

  • Input_Expression: A valid expression that you want to check. For instance, it may be any column you want to operate.
  • When Clause: To test multiple expressions.
  • test_expression: You have to specify an expression. The query will compare the value or expression against the Input_Expression; if it is TRUE, result_expression will return.
  • result_expression: Please provide an expression for the SQL Server case statement. If the test_expression is equal to Input_Expression, then this expression value will return. And if they are not equal, then the default_expression.
  • Boolean_expression: Provide a valid Boolean expression you want to use in searched operation.
  • The optional else statement helps display the default message.

For this SQL Server Case Statement demo, we use EmployeeTb.

Data Source

Types of SQL Case Statements

There are two types of Case Statements, and they are:

SQL Simple Case Statement

The simple case expression compares the input_expression to a series of test_expressions, followed by the WHEN keyword. Once it finds the match, the SQL Server simple CASE Statement will return the corresponding result_expression, followed by the THEN keyword. If there is no match, then it will return the default expression.

SQL Server Simple Case Statement Example

Let us write a simple case statement in SQL Server. The following query will check for each row present in the MyEmployees table whether the condition is met.

  • Occupation = Management, and if it is TRUE, it will assign Administrator as Department Name.
  • Occupation = Professional, and if it is TRUE, it will assign Sr. Software Developer as the Department Name.
  • Occupation = Skilled manual; if TRUE, it assigns Software Developer as Department Name.
  • Else clause – If all the above conditions fail, the Server will assign Fresher as the Department Name
	   WHEN 'Management' THEN 'Administrators'
	   WHEN 'Professional' THEN 'Sr. Software Developer'
	   WHEN 'Skilled Manual' THEN 'Software Developer'
	   ELSE 'Freshers'
	END AS [Department Name]
  FROM [EmployeeTb]
SQL Server CASE Statement 1

How to use Subquery inside a case?

The following query example uses the Subquery inside a Case Statement in SQL Server. First, the Subquery will execute and finds the Average of the Sales amount. Next, it will check whether the Sales are greater than the Average Sales (1970.9055).

And if it is TRUE, ‘This Employee is Performing Good’ is assigned to the Remarks Column. Otherwise, it will assign ‘He is Under Performing’ (else clause). Same as the IF ELSE condition.

-- In Subquery or nested query
      ,[FirstName] + '  ' + [LastName] AS FullName
        WHEN (SELECT AVG([Sales]) FROM [EmployeeTb]) 
                 <= emp.[Sales] THEN 'This Employee is Performing Good'
	ELSE 'He is Under Performing'
	END AS Remarks
FROM [EmployeeTb] AS emp
SQL Server CASE Statement Subquery 2

SQL Searched Case Statement

This is very useful for performing more powerful and complex comparisons. It has a series of Boolean expressions followed by the WHEN keyword and will evaluate the conditions inside them.

  • If the condition is TRUE, it will return result_expression, followed by the THEN keyword.
  • If there is no match, it will return the default expression provided in the ELSE clause block.

Searched SQL Case Statement Example

Let me write a searched Case Statement with Multiple values.

      ,[Position] = 
	   WHEN [Sales] < 100 THEN 'Terminate'
	   WHEN [Sales] >= 100 AND [Sales] <= 600 THEN 'Last Warning'
	   WHEN [Sales] > 600 AND [Sales] < 2000 THEN 'Good Job'
	   WHEN [Sales] >= 2000 AND [Sales] < 4500 THEN 'Great Job - Increase Sales Commission by 1%'
	   ELSE 'Promote as Regional Manager'
  FROM EmployeeTb]
Searched SQL Server CASE Statement 3

SQL Case Statement to Update Table Records

How to update the records in [MyEmployes Table] with new values using the case statement?

The OUTPUT clause returns a copy of the data we inserted, deleted, and updated in our tables. Here, we use the OUTPUT Clause to display the old Yearly Income values and the updated income values in one place.

-- To Update records
UPDATE [EmployeeTb]
SET [YearlyIncome] = (
	   WHEN [Sales] < 100 THEN [YearlyIncome] - 10000
	   WHEN [Sales] >= 100 AND [Sales] <= 600 THEN [YearlyIncome] - 5000
	   WHEN [Sales] > 600 AND [Sales] < 2000 THEN [YearlyIncome] + 5000
	   WHEN [Sales] >= 2000 AND [Sales] < 4500 THEN [YearlyIncome] + 25000
	   ELSE [YearlyIncome] + 55000
OUTPUT inserted.[YearlyIncome] AS [New Income], 
       deleted.[YearlyIncome] AS [Old Income];

Let us see the output of the above SQL Sever Case statement example.

Employee Table After Updating records

Limitations of the case

  • We cannot use this one to control the execution flow of the query, User Defined Functions, and Stored Procedures.
  • Up to 10 levels of nesting expressions are allowed.
  • You cannot use the SQL Server simple case statement to check for NULLs.
  • It performs its operation sequentially. It means when the condition is satisfied, and then it will stop.
Categories SQL