0
0
SQLquery~20 mins

CASE in WHERE clause in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CASE Clause Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of CASE in WHERE clause filtering

Consider a table Employees with columns id, department, and salary. What rows will be returned by this query?

SELECT id, department, salary FROM Employees WHERE CASE WHEN department = 'Sales' THEN salary > 50000 ELSE salary > 70000 END;
SQL
CREATE TABLE Employees (id INT, department VARCHAR(20), salary INT);
INSERT INTO Employees VALUES
(1, 'Sales', 60000),
(2, 'Sales', 40000),
(3, 'HR', 75000),
(4, 'HR', 65000),
(5, 'IT', 80000);
ARows with id 3, 4, and 5
BRows with id 1, 2, 3, and 5
CRows with id 2, 4, and 5
DRows with id 1, 3, and 5
Attempts:
2 left
💡 Hint

Think about how the CASE expression evaluates the condition for each department.

📝 Syntax
intermediate
1:30remaining
Identify syntax error in CASE usage in WHERE clause

Which option contains a syntax error when using CASE in the WHERE clause?

SELECT * FROM Products WHERE CASE category WHEN 'Electronics' THEN price < 100 ELSE price < 50 END;
ACASE category WHEN 'Electronics' THEN price &lt; 100 ELSE price &lt; 50 END
BCASE WHEN category = 'Electronics' THEN price &lt; 100 ELSE price &lt; 50 END
CCASE category WHEN 'Electronics' THEN price &lt; 100 ELSE price &lt; 50
DCASE WHEN category = 'Electronics' THEN price &lt; 100 ELSE price &lt; 50 END;
Attempts:
2 left
💡 Hint

Check if the CASE expression is properly closed.

optimization
advanced
2:30remaining
Optimizing CASE in WHERE clause for performance

You have a large Orders table with columns order_id, status, and amount. You want to filter orders where if status is 'completed', amount > 100, else amount > 200. Which query is likely more efficient?

ASELECT * FROM Orders WHERE status = 'completed' AND amount &gt; 100 AND amount &gt; 200;
BSELECT * FROM Orders WHERE (status = 'completed' AND amount &gt; 100) OR (status != 'completed' AND amount &gt; 200);
CSELECT * FROM Orders WHERE amount &gt; 100 OR amount &gt; 200;
DSELECT * FROM Orders WHERE CASE WHEN status = 'completed' THEN amount &gt; 100 ELSE amount &gt; 200 END;
Attempts:
2 left
💡 Hint

Think about how the database can use indexes with simple conditions.

🧠 Conceptual
advanced
1:30remaining
Understanding CASE evaluation in WHERE clause

What does the CASE expression return in the WHERE clause to filter rows?

SELECT * FROM Customers WHERE CASE WHEN age >= 18 THEN TRUE ELSE FALSE END;
AIt returns TRUE or FALSE to decide if the row is included
BIt returns the age value for filtering
CIt returns NULL for rows where age &lt; 18
DIt returns a string 'TRUE' or 'FALSE' which is always true
Attempts:
2 left
💡 Hint

Remember WHERE expects a condition that evaluates to TRUE or FALSE.

🔧 Debug
expert
3:00remaining
Debugging unexpected results with CASE in WHERE clause

Given this query:

SELECT * FROM Sales WHERE CASE WHEN region = 'North' THEN amount > 1000 ELSE amount > 500 END = TRUE;

Why might this query return no rows even though data exists?

ABecause the CASE expression returns NULL for some rows causing the condition to fail
BBecause the CASE expression returns boolean but comparing with = TRUE is redundant and may cause issues
CBecause the syntax of CASE in WHERE clause is invalid
DBecause amount column has NULL values causing errors
Attempts:
2 left
💡 Hint

Consider how NULL values affect boolean expressions in SQL.