Bird
0
0

Given the table Employees with column department having values 'HR', 'Sales', 'IT', what will be the order of departments after running this query?

medium📝 query result Q4 of 15
SQL - CASE Expressions
Given the table Employees with column department having values 'HR', 'Sales', 'IT', what will be the order of departments after running this query?

SELECT department FROM Employees ORDER BY CASE department WHEN 'Sales' THEN 1 WHEN 'HR' THEN 2 ELSE 3 END;
ASales, HR, IT
BHR, Sales, IT
CIT, HR, Sales
DSales, IT, HR
Step-by-Step Solution
Solution:
  1. Step 1: Understand CASE values assigned

    'Sales' gets 1, 'HR' gets 2, others (like 'IT') get 3.
  2. Step 2: Determine order by numeric values

    Rows with 1 come first, then 2, then 3, so order is Sales, HR, IT.
  3. Final Answer:

    Sales, HR, IT -> Option A
  4. Quick Check:

    CASE assigns order numbers = Sales first [OK]
Quick Trick: Lower CASE values appear earlier in ORDER BY [OK]
Common Mistakes:
  • Assuming alphabetical order instead of CASE order
  • Mixing up numeric values assigned
  • Ignoring ELSE clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes