0
0
SQLquery~20 mins

CASE in ORDER BY in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CASE in ORDER BY Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A[('Bob', 'HR'), ('Alice', 'Sales'), ('Diana', 'Sales'), ('Eve', 'HR'), ('Charlie', 'Engineering')]
B[('Alice', 'Sales'), ('Diana', 'Sales'), ('Bob', 'HR'), ('Eve', 'HR'), ('Charlie', 'Engineering')]
C[('Charlie', 'Engineering'), ('Bob', 'HR'), ('Eve', 'HR'), ('Alice', 'Sales'), ('Diana', 'Sales')]
D[('Bob', 'HR'), ('Eve', 'HR'), ('Alice', 'Sales'), ('Diana', 'Sales'), ('Charlie', 'Engineering')]
Attempts:
2 left
💡 Hint
Think about how the CASE expression assigns numeric values to departments for sorting.
📝 Syntax
intermediate
2: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;
AORDER BY CASE WHEN department = 'HR' THEN 1 ELSE END
BORDER BY CASE department WHEN 'HR' THEN 1 ELSE 2 END
CORDER BY CASE WHEN department = 'HR' THEN 1 ELSE 2 END
DORDER BY CASE department WHEN 'HR' THEN 1 ELSE 2 END ASC
Attempts:
2 left
💡 Hint
Look for incomplete CASE expressions.
optimization
advanced
2: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'?
AORDER BY CASE status WHEN 'Pending' THEN 1 WHEN 'Shipped' THEN 2 ELSE 3 END
BORDER BY status = 'Pending' DESC, status = 'Shipped' DESC, status = 'Cancelled' DESC
CORDER BY FIELD(status, 'Pending', 'Shipped', 'Cancelled')
DORDER BY CASE WHEN status = 'Pending' THEN 1 WHEN status = 'Shipped' THEN 2 ELSE 3 END
Attempts:
2 left
💡 Hint
Consider built-in functions optimized for sorting by specific values.
query_result
advanced
2: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;
A[1, 4, 2, 5, 3]
B[4, 1, 2, 3, 5]
C[2, 1, 4, 3, 5]
D[1, 4, 2, 3, 5]
Attempts:
2 left
💡 Hint
First order by CASE result, then by price descending within groups.
🔧 Debug
expert
3:00remaining
Debug unexpected ORDER BY CASE behavior
A developer wrote this query to order customers by priority:

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?
AThe CASE expression is missing ELSE clause causing NULLs to be sorted first.
BThe DESC applies to the entire CASE result, reversing the order so 3 comes first, showing 'Low' priority first.
CThe priority column has trailing spaces causing CASE to fail matching values.
DThe query needs GROUP BY to order correctly by priority.
Attempts:
2 left
💡 Hint
Think about how DESC affects numeric values from CASE.