0
0
SQLquery~10 mins

CASE in ORDER BY in SQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to order the results by the CASE expression.

SQL
SELECT name, score FROM players ORDER BY CASE WHEN score > 50 THEN [1] ELSE score END;
Drag options to blanks, or click blank then click option'
A1
B0
Cname
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'score' inside the CASE instead of a constant value.
Returning 1 for high scores which orders them after others.
2fill in blank
medium

Complete the code to order by priority using CASE, putting 'urgent' first.

SQL
SELECT task, priority FROM tasks ORDER BY CASE priority WHEN 'urgent' THEN [1] ELSE 2 END;
Drag options to blanks, or click blank then click option'
A5
B3
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a higher number like 5 which orders 'urgent' last.
Using 0 which might be confusing but is valid; here 1 is chosen for clarity.
3fill in blank
hard

Fix the error in the CASE expression inside ORDER BY to sort by status.

SQL
SELECT id, status FROM orders ORDER BY CASE status WHEN 'shipped' THEN [1] WHEN 'pending' THEN 2 ELSE 3 END;
Drag options to blanks, or click blank then click option'
A1
B'1'
Cstatus
D'shipped'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning strings like '1' instead of numeric 1.
Returning the column name instead of a value.
4fill in blank
hard

Fill both blanks to order by category priority and then by name alphabetically.

SQL
SELECT product, category FROM inventory ORDER BY CASE category WHEN 'electronics' THEN [1] WHEN 'furniture' THEN [2] ELSE 3 END, name;
Drag options to blanks, or click blank then click option'
A1
B2
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the same number to both categories causing no priority difference.
Using numbers that do not reflect intended priority order.
5fill in blank
hard

Fill all three blanks to order employees by role priority, then by last name ascending.

SQL
SELECT employee_id, role, last_name FROM employees ORDER BY CASE role WHEN 'manager' THEN [1] WHEN 'developer' THEN [2] ELSE [3] END, last_name;
Drag options to blanks, or click blank then click option'
A1
B2
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning higher numbers to managers which lowers their priority.
Using the same number for all roles causing no ordering.