How to set up and query conditions with SQL CASE

SQL CASE lets you manipulate a database by querying certain conditions and displaying the results. The approach of the CASE condition is similar to the If-then-else statement.

What is SQL CASE?

The If-then-else statement is common in many database and programming languages. It executes an action if a certain condition is met. If the condition is no longer met, the program exits the loop or executes another action. The Structured Query Language is based on a similar principle and provides the SQL CASE statement. This statement evaluates a list of conditions and returns a specific value if a condition is met. If no conditions are met, it returns a value defined under ELSE. If there is no ELSE clause and the conditions are not met, it outputs NULL.

VPS Hosting
Fully virtualised servers with root access
  • Unlimited traffic and up to 1 Gbit/s bandwidth
  • Fast SSD NVMe storage
  • Free Plesk Web Host Edition

Syntax and function

The basic syntax of SQL CASE is as follows:

SELECT column1, column2, ..., 
CASE 
WHEN condition1 THEN result1 
END AS name 
FROM name_of_table;
sql

Use the SQL command SELECT to specify which columns should be taken into account. Use CASE to check the condition. If this applies (i.e., it is true), the value of the stored result is output. END AS ends the loop. FROM specifies the table in which this loop is to be executed.

With the optional ELSE restriction, the syntax is:

SELECT column1, column2, ..., 
CASE 
WHEN condition1 THEN result1 
ELSE other_result 
END AS name 
FROM name_of_table;
sql

Example for using the condition

To illustrate how SQL CASE works, we have created a simple table called ‘Customer list’. This table stores the customer number, customer name, location, order date, number of products ordered, and the amount paid in pounds.

Customer number Name Location Date Product Sum
1427 Smith Aberystwyth 1/13/2024 14 634
1377 Johnson Luton 1/19/2024 9 220
1212 Brown Luton 1/3/2024 15 400
1431 Miller Aberdeen 1/19/2024 22 912
1118 Davis Bath 2/1/2024 10 312

Now we use the condition to work through the list. A specific text should be output for total amounts over 400 pounds, and another for amounts under 400 pounds. We use the ELSE clause for orders that are exactly 400 pounds. This demonstrates how multiple conditions can be checked in sequence. The appropriate syntax for our example is as follows:

SELECT Customer number, name, location, date, product sum
CASE
	WHEN Sum > 400 THEN 'The purchase value is over 400 Pounds'
	WHEN Sum < 400 THEN 'The purchase value is less than 400 Pounds'
	ELSE 'The purchase value is exactly 400 Pounds'
END AS Order details
FROM Customer list;
sql

The output looks as follows:

Customer number Name Location Date Product Sum Order details
1427 Smith Aberystwyth 1/13/2024 14 634 The amount is over 400 pounds
1377 Johnson Luton 1/19/2024 9 220 The amount is under 400 pounds
1212 Brown Luton 1/3/2024 15 400 The amount is exactly 400 pounds
1431 Miller Aberdeen 1/19/2024 22 912 The amount is over 400 pounds
1118 Davis Bath 2/1/2024 10 312 The amount is under 400 pounds

Combination with other instructions

You can also query the conditions within other instructions. In the following, we arrange the entries by customer number. If this isn’t specified (i.e. NULL), the customer name should be taken into account instead:

SELECT Customer number, name, location, date, product, sum
FROM Customer list
ORDER BY
(CASE
		WHEN Customer number IS NULL THEN Name
		ELSE Customer number
END);
sql

The output will be:

Customer number Name Location Date Product Sum
1118 Davis Bath 2/1/2024 10 312
1212 Brown Luton 1/3/2024 15 400
1377 Johnson Luton 1/19/2024 9 220
1427 Smith Aberystwyth 1/13/2024 14 634
1431 Miller Aberdeen 1/19/2024 22 912
Tip

With SQL Server Hosting from IONOS you select between MSSQL, MySQL or MariaDB. Benefit from exceptional performance, a strong security architecture and personal advice!

Was this article helpful?
Page top