Stop 04 · Functions & ExpressionsTopics · 6/8

CASE WHEN Expressions

Part of the SQL Roadmap

In short

Raw data says amount = 2,450. Your manager wants "high", "medium" or "low". CASE WHEN is SQL's if-else, turning values into labels, buckets and flags inside any query.

CASE WHEN is SQL's if-else. It returns different values based on conditions, which lets you bucket, label or flag rows inside a query.

The jaadu sorts a stack of answer sheets into four labelled trays on a teacher's desk, one sheet mid-air.

Think of it like this

Marks into grades

A teacher checks each answer sheet against a list: 90 and above is A, 75 and above is B, 50 and above is C, otherwise F. She stops at the first rule that fits. CASE works top to bottom the same way, and returns the label of the first true condition.

Watch it happen

An order of ₹2,450 passes through CASE

amount
>= 5000?
>= 1000?
'medium'
Done

1/5The row's amount is 2,450.

The big ideas

What you need to know

First match wins

Conditions are tested in order. Put the most specific or highest threshold first.

ELSE is the fallback

Without ELSE, rows that match nothing get NULL. Always add one so nothing slips through unlabelled.

Works anywhere an expression does

In SELECT for labels, in ORDER BY for custom sort order, and inside SUM or COUNT for conditional totals.

Two forms

Searched: CASE WHEN amount > 1000 THEN ... Simple: CASE status WHEN 'paid' THEN ... for plain equality.

See it in code

Walk through the code

sql
1SELECT2  order_id,3  amount,4  CASE5    WHEN amount >= 5000 THEN 'high'6    WHEN amount >= 1000 THEN 'medium'7    ELSE 'low'8  END AS order_size,9  CASE status10    WHEN 'paid'     THEN 'Payment done'11    WHEN 'pending'  THEN 'Awaiting UPI'12    ELSE 'Check manually'13  END AS status_label14FROM orders15ORDER BY CASE WHEN status = 'pending' THEN 0 ELSE 1 END, amount DESC;

1/3A searched CASE: three size buckets, highest threshold first.

Standard SQL that runs unchanged in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

Ordering conditions low to high: WHEN amount >= 1000 before WHEN amount >= 5000.

Do this instead

A ₹6,000 order matches the first rule and becomes 'medium'. Put the highest threshold first.

Oops

Leaving out ELSE.

Do this instead

Unmatched rows get NULL and quietly vanish from later counts. Add ELSE 'other'.

Oops

Returning text in one branch and a number in another.

Do this instead

Every THEN must return the same type. Use '0' or cast so they match.

Your mission

Label your data

~20 min

Quick check

With CASE WHEN marks >= 50 THEN 'Pass' WHEN marks >= 90 THEN 'Distinction' END, what does a score of 95 get?

Go deeper

  1. 1Label orders as Small, Medium or Large based on amount.
  2. 2Always add an ELSE so unexpected values don't silently become NULL.
  3. 3Remember that conditions are checked in order and the first match wins.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge