Challenge - 5 Problems
CASE in ORDER BY Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of CASE in ORDER BY with simple conditions
Consider a table Employees with columns
id, name, and department. What is the output order of names after running this query?SELECT name, department FROM Employees ORDER BY CASE department WHEN 'HR' THEN 1 WHEN 'Sales' THEN 2 ELSE 3 END;
SQL
CREATE TABLE Employees (id INT, name VARCHAR(20), department VARCHAR(20)); INSERT INTO Employees VALUES (1, 'Alice', 'Sales'), (2, 'Bob', 'HR'), (3, 'Charlie', 'Engineering'), (4, 'Diana', 'Sales'), (5, 'Eve', 'HR'); SELECT name, department FROM Employees ORDER BY CASE department WHEN 'HR' THEN 1 WHEN 'Sales' THEN 2 ELSE 3 END;
Attempts:
2 left
💡 Hint
Think about how the CASE expression assigns numeric values to departments for sorting.
✗ Incorrect
The CASE expression assigns 1 to 'HR', 2 to 'Sales', and 3 to others. ORDER BY sorts ascending by these values, so HR employees come first, then Sales, then Engineering.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in CASE used in ORDER BY
Which option contains a syntax error when using CASE in ORDER BY clause?
SQL
SELECT id, name FROM Employees ORDER BY CASE WHEN department = 'HR' THEN 1 ELSE 2 END;
Attempts:
2 left
💡 Hint
Look for incomplete CASE expressions.
✗ Incorrect
Option A is missing the value after ELSE, causing a syntax error. All others are valid CASE expressions.
❓ optimization
advanced2:00remaining
Optimizing ORDER BY CASE for performance
Given a large table
Orders with a status column having values 'Pending', 'Shipped', 'Cancelled', which ORDER BY clause is most efficient to sort rows with 'Pending' first, then 'Shipped', then 'Cancelled'?Attempts:
2 left
💡 Hint
Consider built-in functions optimized for sorting by specific values.
✗ Incorrect
The FIELD function (MySQL) is optimized for sorting by specific values and can be faster than CASE expressions. Options A and D are similar CASE expressions. Option C uses boolean expressions which may be less efficient.
❓ query_result
advanced2:00remaining
Result of ORDER BY CASE with multiple conditions
Given a table
Products with columns id, category, and price, what is the order of id after running this query?SELECT id FROM Products ORDER BY CASE WHEN category = 'Electronics' AND price > 1000 THEN 1 WHEN category = 'Electronics' THEN 2 ELSE 3 END, price DESC;
SQL
CREATE TABLE Products (id INT, category VARCHAR(20), price INT); INSERT INTO Products VALUES (1, 'Electronics', 1500), (2, 'Electronics', 800), (3, 'Clothing', 500), (4, 'Electronics', 1200), (5, 'Clothing', 700); SELECT id FROM Products ORDER BY CASE WHEN category = 'Electronics' AND price > 1000 THEN 1 WHEN category = 'Electronics' THEN 2 ELSE 3 END, price DESC;
Attempts:
2 left
💡 Hint
First order by CASE result, then by price descending within groups.
✗ Incorrect
Rows with category 'Electronics' and price > 1000 get 1, then other 'Electronics' get 2, others get 3. Within each group, order by price descending. So ids 1 and 4 (both Electronics > 1000) come first ordered by price desc (1500 then 1200), then id 2 (Electronics 800), then clothing ids 3 and 5 ordered by price desc (700 then 500).
🔧 Debug
expert3:00remaining
Debug unexpected ORDER BY CASE behavior
A developer wrote this query to order customers by priority:
But the results show 'Low' priority customers first. What is the cause?
SELECT customer_id, priority FROM Customers ORDER BY CASE priority WHEN 'High' THEN 1 WHEN 'Medium' THEN 2 ELSE 3 END DESC;
But the results show 'Low' priority customers first. What is the cause?
Attempts:
2 left
💡 Hint
Think about how DESC affects numeric values from CASE.
✗ Incorrect
The CASE assigns 1 to 'High', 2 to 'Medium', 3 to others. ORDER BY ... DESC reverses order, so 3 (Low) comes first, then 2, then 1. This causes 'Low' priority customers to appear first.