We use CASE in the WHERE clause to choose different filtering rules based on conditions. It helps us make flexible queries that change what rows to pick depending on data.
CASE in WHERE clause in SQL
SELECT columns FROM table_name WHERE CASE WHEN condition1 THEN filter_condition1 WHEN condition2 THEN filter_condition2 ELSE filter_condition_default END = TRUE;
The CASE expression returns a value that the WHERE clause uses to filter rows.
Each THEN part should return a boolean condition (TRUE or FALSE) for filtering.
SELECT * FROM employees WHERE CASE WHEN department = 'Sales' THEN salary > 50000 ELSE salary > 30000 END = TRUE;
SELECT * FROM orders WHERE CASE WHEN status = 'Pending' THEN order_date > '2024-01-01' ELSE TRUE END = TRUE;
This query selects products with different price filters based on their category. Electronics must cost more than 500, Clothing more than 100, and others more than 10.
CREATE TABLE products ( id INT, category VARCHAR(20), price DECIMAL(10,2) ); INSERT INTO products VALUES (1, 'Electronics', 1200.00), (2, 'Clothing', 80.00), (3, 'Electronics', 300.00), (4, 'Clothing', 150.00), (5, 'Books', 20.00); SELECT id, category, price FROM products WHERE CASE WHEN category = 'Electronics' THEN price > 500 WHEN category = 'Clothing' THEN price > 100 ELSE price > 10 END = TRUE;
CASE in WHERE helps avoid repeating similar queries with small differences.
Make sure each THEN returns a condition that evaluates to TRUE or FALSE.
Using ELSE TRUE means no filtering for rows that don't match earlier conditions.
CASE in WHERE lets you pick different filters based on data values.
It simplifies queries that need conditional filtering.
Each CASE branch returns a boolean condition for filtering rows.
Practice
CASE in a WHERE clause allow you to do in SQL?Solution
Step 1: Understand the role of CASE in WHERE
The CASE expression in WHERE lets you choose which condition to apply depending on data values in each row.Step 2: Compare with other options
Creating new columns is done with SELECT, sorting with ORDER BY, and joining with JOIN, not CASE in WHERE.Final Answer:
Apply different filter conditions based on row values -> Option CQuick Check:
CASE in WHERE = conditional filtering [OK]
- Thinking CASE creates new columns
- Confusing CASE with ORDER BY
- Assuming CASE controls JOIN logic
CASE inside a WHERE clause?Solution
Step 1: Review correct CASE syntax
The correct CASE syntax uses WHEN condition THEN result ELSE result END. WHERE CASE WHEN age > 18 THEN 'adult' ELSE 'minor' END = 'adult' follows this exactly.Step 2: Identify syntax errors in other options
Options B, C, and D misuse CASE syntax by missing WHEN or ELSE keywords or incorrect placement.Final Answer:
WHERE CASE WHEN age > 18 THEN 'adult' ELSE 'minor' END = 'adult' -> Option BQuick Check:
CASE WHEN ... THEN ... ELSE ... END [OK]
- Omitting WHEN keyword
- Using CASE without END
- Placing conditions incorrectly
Employees with column salary, what rows will this query return?SELECT * FROM Employees WHERE CASE WHEN salary >= 50000 THEN 1 ELSE 0 END = 1;
Solution
Step 1: Understand the CASE condition
The CASE returns 1 if salary is 50000 or more, else 0.Step 2: Apply the WHERE filter
The WHERE clause keeps rows where CASE result equals 1, so salary must be at least 50000.Final Answer:
Rows where salary is 50000 or more -> Option DQuick Check:
salary >= 50000 = filtered rows [OK]
- Thinking it returns rows below 50000
- Confusing equality with greater than
- Ignoring ELSE result
SELECT * FROM Orders WHERE CASE order_status WHEN 'shipped' THEN TRUE ELSE FALSE;
Solution
Step 1: Check CASE syntax completeness
The CASE expression must end with END keyword, which is missing here.Step 2: Verify other parts
TRUE/FALSE usage is valid, CASE can be used in WHERE, and THEN keyword is present.Final Answer:
Missing END keyword for CASE expression -> Option AQuick Check:
CASE ... END required [OK]
- Forgetting END keyword
- Thinking CASE can't be in WHERE
- Misplacing THEN keyword
Products so that if category is 'Electronics', only products with stock > 0 are shown; otherwise, show products with price < 100. Which WHERE clause using CASE is correct?Solution
Step 1: Understand CASE returns boolean condition
The CASE must return TRUE or FALSE to filter rows properly.Step 2: Check syntax and comparison
WHERE CASE WHEN category = 'Electronics' THEN stock > 0 ELSE price < 100 END = TRUE correctly uses WHEN condition THEN boolean expression ELSE boolean expression END = TRUE to filter rows.Step 3: Identify why others are wrong
Options A, B, and D miss the END keyword for the CASE expression, causing syntax errors.Final Answer:
WHERE CASE WHEN category = 'Electronics' THEN stock > 0 ELSE price < 100 END = TRUE -> Option AQuick Check:
CASE returns boolean, compare with TRUE in WHERE [OK]
- Missing END keyword
- Using CASE with simple WHEN without condition
- Returning non-boolean values in CASE
Ready to prove it?
Pass the test and this pattern moves unknown
