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.
- Unlimited traffic
- Fast SSD NVMe storage
- Free Plesk Web Host Edition
Syntax and function
The basic syntax of SQL CASE is as follows:
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:
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 dollars.
Customer number | Name | Location | Date | Product | Sum |
---|---|---|---|---|---|
1427 | Smith | New York | 1/13/2024 | 14 | 634 |
1377 | Johnson | Chicago | 1/19/2024 | 9 | 220 |
1212 | Brown | Chicago | 1/3/2024 | 15 | 400 |
1431 | Miller | Los Angeles | 1/19/2024 | 22 | 912 |
1118 | Davis | Dallas | 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 dollars, and another for amounts under 400 dollars. We use the ELSE clause for orders that are exactly 400 dollars. This demonstrates how multiple conditions can be checked in sequence. The appropriate syntax for our example is as follows:
The output looks as follows:
Customer number | Name | Location | Date | Product | Sum | Order details |
---|---|---|---|---|---|---|
1427 | Smith | New York | 1/13/2024 | 14 | 634 | The amount is over 400 dollars |
1377 | Johnson | Chicago | 1/19/2024 | 9 | 220 | The amount is under 400 dollars |
1212 | Brown | Chicago | 1/3/2024 | 15 | 400 | The amount is exactly 400 dollars |
1431 | Miller | Los Angeles | 1/19/2024 | 22 | 912 | The amount is over 400 dollars |
1118 | Davis | Dallas | 2/1/2024 | 10 | 312 | The amount is under 400 dollars |
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:
The output will be:
Customer number | Name | Location | Date | Product | Sum |
---|---|---|---|---|---|
1118 | Davis | Dallas | 2/1/2024 | 10 | 312 |
1212 | Brown | Chicago | 1/3/2024 | 15 | 400 |
1377 | Johnson | Chicago | 1/19/2024 | 9 | 220 |
1427 | Smith | New York | 1/13/2024 | 14 | 634 |
1431 | Miller | Los Angeles | 1/19/2024 | 22 | 912 |
With SQL Server Hosting from IONOS you select between MSSQL, MySQL or MariaDB. Benefit from exceptional performance, a strong security architecture and personal advice!