Bird
Raised Fist0
SQLquery~5 mins

CASE in ORDER BY in SQL - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the CASE statement do in an ORDER BY clause?
It allows you to customize the sorting order by specifying different conditions and corresponding sort priorities.
Click to reveal answer
beginner
Write a simple example of using CASE in ORDER BY to sort by a column 'status' where 'urgent' comes first, then 'normal', then others.
ORDER BY CASE status WHEN 'urgent' THEN 1 WHEN 'normal' THEN 2 ELSE 3 END
Click to reveal answer
intermediate
Why use CASE in ORDER BY instead of just ordering by a column directly?
Because CASE lets you define a custom order that is not alphabetical or numerical, like prioritizing certain values over others.
Click to reveal answer
intermediate
Can CASE in ORDER BY handle multiple conditions?
Yes, you can use multiple WHEN conditions to assign different sort priorities based on complex logic.
Click to reveal answer
intermediate
What happens if no WHEN condition matches in a CASE inside ORDER BY?
The ELSE part defines the default sort priority; if ELSE is missing, NULL is used which may affect sorting.
Click to reveal answer
What is the purpose of using CASE in an ORDER BY clause?
ATo group rows by a condition
BTo filter rows before sorting
CTo create a custom sorting order based on conditions
DTo rename columns in the result
Which keyword is used to specify the default sorting priority if no CASE condition matches?
ADEFAULT
BELSE
CWHEN
DTHEN
How does the following ORDER BY clause sort rows? ORDER BY CASE priority WHEN 'high' THEN 1 WHEN 'medium' THEN 2 ELSE 3 END
ABy numeric priority: high first, then medium, then others
BAlphabetically by priority
CRandom order
DBy length of the priority string
Can CASE in ORDER BY be used to sort by multiple columns?
ANo, only one column at a time
BOnly if columns are text
COnly if columns are numeric
DYes, by combining multiple CASE expressions
If ELSE is omitted in CASE inside ORDER BY, what is the default behavior?
ARows with unmatched conditions get NULL and sort accordingly
BRows with unmatched conditions cause an error
CRows with unmatched conditions are sorted last
DRows with unmatched conditions are excluded
Explain how to use CASE in ORDER BY to prioritize sorting of specific values in a column.
Think about assigning numbers to values to control their order.
You got /4 concepts.
    Describe what happens when no WHEN condition matches in a CASE expression used in ORDER BY.
    Consider what value CASE returns if no condition matches.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the CASE statement do when used inside an ORDER BY clause in SQL?
      easy
      A. It filters rows before sorting them.
      B. It allows sorting rows based on custom conditions you define.
      C. It changes the data type of the sorted column.
      D. It groups rows before sorting.

      Solution

      1. Step 1: Understand the role of CASE in ORDER BY

        The CASE statement inside ORDER BY assigns sorting values conditionally to each row.
      2. Step 2: Differentiate from filtering or grouping

        CASE does not filter or group rows; it only affects sorting order.
      3. Final Answer:

        It allows sorting rows based on custom conditions you define. -> Option B
      4. Quick Check:

        CASE in ORDER BY = custom sorting [OK]
      Hint: CASE in ORDER BY controls sorting order by conditions [OK]
      Common Mistakes:
      • Thinking CASE filters rows
      • Assuming CASE changes data types
      • Confusing CASE with GROUP BY
      2. Which of the following is the correct syntax to use CASE inside an ORDER BY clause?
      easy
      A. ORDER BY CASE WHEN condition THEN value ELSE value END
      B. ORDER BY IF condition THEN value ELSE value END
      C. ORDER BY SWITCH condition THEN value ELSE value END
      D. ORDER BY SELECT CASE condition THEN value ELSE value END

      Solution

      1. Step 1: Recall correct CASE syntax in ORDER BY

        The correct syntax uses CASE WHEN ... THEN ... ELSE ... END without extra keywords.
      2. Step 2: Identify incorrect keywords

        Options A, B, and C use invalid keywords like SELECT, IF, or SWITCH which are not valid in ORDER BY CASE.
      3. Final Answer:

        ORDER BY CASE WHEN condition THEN value ELSE value END -> Option A
      4. Quick Check:

        CASE syntax = WHEN THEN ELSE END [OK]
      Hint: Use CASE WHEN ... THEN ... ELSE ... END in ORDER BY [OK]
      Common Mistakes:
      • Using IF instead of CASE
      • Adding SELECT inside ORDER BY
      • Using SWITCH keyword
      3. Given the table Tasks with column priority having values 'High', 'Medium', 'Low', what will the query below return?

      SELECT priority FROM Tasks ORDER BY CASE priority WHEN 'High' THEN 1 WHEN 'Medium' THEN 2 ELSE 3 END;
      medium
      A. Rows sorted with 'High' first, then 'Medium', then 'Low'.
      B. Rows sorted alphabetically by priority.
      C. Rows sorted with 'Low' first, then 'Medium', then 'High'.
      D. Syntax error due to CASE in ORDER BY.

      Solution

      1. Step 1: Understand CASE assigns sorting values

        CASE assigns 1 to 'High', 2 to 'Medium', and 3 to others (like 'Low').
      2. Step 2: ORDER BY sorts ascending by these values

        Rows with priority 'High' come first, then 'Medium', then 'Low'.
      3. Final Answer:

        Rows sorted with 'High' first, then 'Medium', then 'Low'. -> Option A
      4. Quick Check:

        CASE assigns order numbers = sorted by priority [OK]
      Hint: CASE assigns numbers to priorities to sort custom order [OK]
      Common Mistakes:
      • Assuming alphabetical sort
      • Thinking CASE causes syntax error
      • Confusing order direction
      4. Identify the error in this SQL query:

      SELECT name FROM Employees ORDER BY CASE WHEN department = 'Sales' THEN 1 WHEN department = 'HR' THEN 2 ELSE 3;
      medium
      A. ORDER BY cannot use CASE statements.
      B. Incorrect use of ELSE in CASE.
      C. Missing END keyword to close CASE statement.
      D. Missing THEN keyword after conditions.

      Solution

      1. Step 1: Check CASE syntax completeness

        The CASE statement must end with the keyword END to close it.
      2. Step 2: Identify missing END keyword

        The query lacks END after ELSE 3, causing syntax error.
      3. Final Answer:

        Missing END keyword to close CASE statement. -> Option C
      4. Quick Check:

        CASE must end with END [OK]
      Hint: Always close CASE with END keyword [OK]
      Common Mistakes:
      • Omitting END keyword
      • Misplacing ELSE clause
      • Thinking CASE not allowed in ORDER BY
      5. You want to sort products so that 'Electronics' appear first, 'Clothing' second, and all others last. Which ORDER BY clause using CASE achieves this?
      hard
      A. ORDER BY CASE category WHEN 'Clothing' THEN 1 WHEN 'Electronics' THEN 2 ELSE 3 END
      B. ORDER BY CASE WHEN category = 'Electronics' THEN 3 WHEN category = 'Clothing' THEN 2 ELSE 1 END
      C. ORDER BY category = 'Electronics' DESC, category = 'Clothing' DESC
      D. ORDER BY CASE category WHEN 'Electronics' THEN 1 WHEN 'Clothing' THEN 2 ELSE 3 END

      Solution

      1. Step 1: Assign lowest number to 'Electronics' for first position

        ORDER BY CASE category WHEN 'Electronics' THEN 1 WHEN 'Clothing' THEN 2 ELSE 3 END assigns 1 to 'Electronics', 2 to 'Clothing', and 3 to others, sorting correctly.
      2. Step 2: Check other options for order correctness

        ORDER BY CASE WHEN category = 'Electronics' THEN 3 WHEN category = 'Clothing' THEN 2 ELSE 1 END reverses order numbers, ORDER BY CASE category WHEN 'Clothing' THEN 1 WHEN 'Electronics' THEN 2 ELSE 3 END swaps categories, ORDER BY category = 'Electronics' DESC, category = 'Clothing' DESC uses boolean sorting which may not guarantee order.
      3. Final Answer:

        ORDER BY CASE category WHEN 'Electronics' THEN 1 WHEN 'Clothing' THEN 2 ELSE 3 END -> Option D
      4. Quick Check:

        Lowest number = highest priority in CASE [OK]
      Hint: Assign 1 to highest priority category in CASE [OK]
      Common Mistakes:
      • Swapping priority numbers
      • Using boolean expressions without CASE
      • Forgetting ELSE clause
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard