How do I perform an IF...THEN in an SQL SELECT?
Conditional Logic in SQL SELECT: Mastering IF...THEN with CASE

Learn how to implement IF...THEN logic directly within your SQL SELECT statements using the powerful CASE expression, enhancing data presentation and analysis.
In SQL, the ability to apply conditional logic directly within a SELECT
statement is incredibly powerful. While many programming languages offer explicit IF...THEN
constructs, SQL achieves this primarily through the CASE
expression. This article will guide you through using CASE
to perform conditional evaluations, allowing you to transform data, categorize results, and generate dynamic outputs based on specific criteria, all within a single query.
Understanding the SQL CASE Expression
The CASE
expression is SQL's equivalent of an IF...THEN...ELSE
statement. It allows you to define different outcomes based on various conditions. There are two main forms of the CASE
expression: Simple CASE
and Searched CASE
.
flowchart TD A[Start SELECT Statement] --> B{Evaluate CASE Expression} B --> C{Condition 1 Met?} C -->|Yes| D[Result 1] C -->|No| E{Condition 2 Met?} E -->|Yes| F[Result 2] E -->|No| G{No Conditions Met?} G -->|Yes| H[ELSE Result] G -->|No| I[Continue to next condition] D --> J[End CASE] F --> J[End CASE] H --> J[End CASE] J --> K[Return Final Value]
Flowchart illustrating the logic of a SQL CASE expression
Simple CASE Expression
The simple CASE
expression compares a single expression to a set of possible values. It's concise when you're checking for equality against a specific column or value.
SELECT
ProductName,
Category,
CASE Category
WHEN 'Electronics' THEN 'High-Tech'
WHEN 'Books' THEN 'Literary'
ELSE 'General Merchandise'
END AS CategoryType
FROM
Products;
Example of a Simple CASE expression categorizing products.
ELSE
clause in a CASE
expression is optional, but highly recommended. If omitted and no WHEN
condition is met, the CASE
expression will return NULL
.Searched CASE Expression
The searched CASE
expression is more flexible. It allows you to specify different boolean conditions for each WHEN
clause, similar to a series of IF...ELSE IF...ELSE
statements in procedural programming. This is ideal for range checks, multiple column comparisons, or complex logical evaluations.
SELECT
EmployeeName,
Salary,
CASE
WHEN Salary >= 100000 THEN 'High Earner'
WHEN Salary >= 50000 AND Salary < 100000 THEN 'Mid-Range Earner'
WHEN Salary < 50000 THEN 'Entry-Level Earner'
ELSE 'Salary Not Available'
END AS SalaryBracket
FROM
Employees;
Example of a Searched CASE expression determining salary brackets.
WHEN
clauses matters in a searched CASE
expression. The first condition that evaluates to TRUE
will be executed, and subsequent conditions will be ignored. Always place more specific conditions before more general ones.Practical Applications and Best Practices
The CASE
expression is incredibly versatile. You can use it for:
- Data Transformation: Converting numeric codes to descriptive labels.
- Conditional Aggregation: Counting or summing based on specific criteria within groups.
- Dynamic Ordering: Sorting results based on a calculated value.
- Reporting: Creating custom columns for reports that summarize data conditionally.
When using CASE
, aim for clarity. Use meaningful aliases for your CASE
columns, and keep your conditions as straightforward as possible. For very complex logic, consider breaking it down into multiple CASE
statements or using views/subqueries to improve readability and maintainability.
SELECT
OrderDate,
SUM(CASE WHEN OrderStatus = 'Completed' THEN OrderTotal ELSE 0 END) AS TotalCompletedSales,
SUM(CASE WHEN OrderStatus = 'Pending' THEN OrderTotal ELSE 0 END) AS TotalPendingSales
FROM
Orders
GROUP BY
OrderDate;
Using CASE for conditional aggregation to sum sales by status.