Ravindra BagaleCourses & study guides Track your progress

29. SQL for Excel Users (Term Map + Side-by-Side)

29.4 CASE WHEN = IF / IFS

Why. IF / IFS in Excel = CASE WHEN in SQL.

What. Same rule engine; SQL keeps the rule next to the data.

How. Translate one IFS into CASE on order size.

Steps

  1. Write IFS in Excel words.
  2. Write CASE WHEN.
  3. Compare labels on the same amounts.

Examples

Example 1 · dataset: raja_coffee

SELECT amount_inr,
  CASE WHEN amount_inr >= 1000 THEN 'Large'
       WHEN amount_inr >= 600 THEN 'Medium'
       ELSE 'Small' END AS size
FROM orders
LIMIT 6;

Output

amount_inrsize
840.0Medium
720.0Medium
890.0Medium
1050.0Large
620.0Medium
890.0Medium

Meaning. IFS twin.

Ravindra Bagale's Tip

Prefer CASE in SQL over exporting to Excel just to IF-label.