SQL Learning Hub
SQL CASE WHEN Expression
SQL CASE WHEN Expression
Learn how to implement conditional logic in SQL queries with CASE WHEN expressions for data transformation and classification
Understanding the SQL CASE WHEN Expression
The CASE WHEN expression in SQL provides a way to add conditional logic to your queries. It works like an if-then-else statement, allowing you to evaluate conditions and return different values based on the results.
Basic Syntax
Common SQL CASE WHEN Interview Questions
- What are the two forms of CASE expressions and when would you use each?
- How does CASE WHEN handle NULL values?
- Can you use CASE WHEN in different parts of a SQL query?
- How do you use CASE WHEN with aggregate functions?
Two Forms of CASE Expressions
Simple CASE Expression
The simple CASE form compares an expression to a set of values for equality. Simple CASE is useful when comparing a single expression against multiple possible values.
Searched CASE Expression
The searched CASE form evaluates a list of conditions and returns a result for the first condition that is true. Searched CASE is useful when evaluating different expressions with different operators in each WHEN clause.
CASE Evaluation Order
CASE evaluates conditions in order and returns the result for the first matching condition.
Important: If multiple conditions are true, only the result for the first true condition is returned. In the example below, a video with 15000 views matches the first condition, so 'Highly Popular' is returned.
Using CASE WHEN in Different SQL Clauses
CASE in SELECT Clause
The most common use of CASE is in the SELECT clause to transform output values.
CASE in WHERE Clause
You can use CASE in a WHERE clause for complex filtering logic.
Note: While this works, complex conditions in WHERE clauses are often better expressed using normal Boolean logic with AND/OR operators.
CASE in ORDER BY Clause
CASE is powerful in ORDER BY clauses for custom sorting logic.
CASE in GROUP BY Clause
You can use CASE to create dynamic categories for grouping.
CASE WHEN with Aggregate Functions
Conditional Counting
Use CASE with COUNT to count rows that meet specific conditions.
Note: We use COUNT(CASE WHEN...) rather than SUM(CASE WHEN...) because COUNT ignores NULLs, which is what we want for rows that don't match our condition.
Conditional Summing
Use CASE with SUM to calculate totals based on conditions.
Pivot Tables with CASE
Create pivot tables by combining CASE with aggregate functions.
Practical SQL CASE WHEN Examples
Example 1: Dynamic Date Formatting
This example shows how to format dates differently based on how recent they are:
Example 2: User Engagement Classification
This example creates an engagement score and classification for users:
Example 3: Complex Performance Analysis
This example uses CASE expressions for detailed performance analysis:
Best Practices for CASE WHEN
1. Maintain Consistent Return Types
All result expressions in a CASE statement should have compatible data types. The SQL engine implicitly converts them to a common type, but explicit control is better for readability and preventing unexpected conversions.
2. Include an ELSE Clause
Always include an ELSE clause to handle unexpected cases. Without ELSE, unmatched conditions return NULL, which may not be your intended default behavior.
3. Order Conditions Appropriately
Remember that CASE evaluates conditions in order and returns the result for the first matching condition. Put more specific conditions before more general ones.
4. Keep CASE Expressions Simple
For very complex logic, consider using views or CTEs (Common Table Expressions) to break down the logic into more manageable pieces rather than creating extremely complex CASE expressions.
Test Your Knowledge
Take a quick quiz to reinforce what you've learned about SQL CASE WHEN Expression
Ready for hands-on SQL practice?
We have 200+ questions on real companies and real products.