Bird
0
0

Consider the table Products with column category having values 'Electronics', 'Clothing', 'Books'. What will be the order of categories after this query?

medium📝 query result Q5 of 15
SQL - CASE Expressions
Consider the table Products with column category having values 'Electronics', 'Clothing', 'Books'. What will be the order of categories after this query?

SELECT category FROM Products ORDER BY CASE WHEN category = 'Books' THEN 3 WHEN category = 'Electronics' THEN 1 ELSE 2 END;
AClothing, Electronics, Books
BBooks, Electronics, Clothing
CElectronics, Books, Clothing
DElectronics, Clothing, Books
Step-by-Step Solution
Solution:
  1. Step 1: Map CASE values to categories

    'Electronics' = 1, 'Clothing' = 2 (ELSE), 'Books' = 3.
  2. Step 2: Sort by assigned values

    Order is 1 (Electronics), 2 (Clothing), 3 (Books).
  3. Final Answer:

    Electronics, Clothing, Books -> Option D
  4. Quick Check:

    CASE numeric order = Electronics first [OK]
Quick Trick: CASE assigns numeric sort keys, lower first [OK]
Common Mistakes:
  • Assuming alphabetical order
  • Misreading WHEN conditions
  • Ignoring ELSE clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes