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;
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);
Think about how the CASE expression evaluates the condition for each department.
The CASE expression checks if the department is 'Sales'. For 'Sales', it filters salaries > 50000 (id 1 passes, id 2 fails). For others, it filters salaries > 70000 (id 3 and 5 pass, id 4 fails).
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;
Check if the CASE expression is properly closed.
Option C is missing the END keyword to close the CASE expression, causing a syntax error.
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?
Think about how the database can use indexes with simple conditions.
Option B uses explicit AND/OR conditions which can better use indexes. Option B uses CASE which may prevent index use. Options C and D do not correctly implement the logic.
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;
Remember WHERE expects a condition that evaluates to TRUE or FALSE.
The CASE expression returns a boolean TRUE or FALSE. WHERE includes rows where the condition is TRUE.
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?
Consider how NULL values affect boolean expressions in SQL.
If amount is NULL, the CASE expression returns NULL, and comparing NULL = TRUE yields FALSE, so those rows are excluded.