SQL Topic
CASE WHEN
CASE WHEN evaluates conditions in order and returns the first match — SQL's if/else. It handles conditional logic, bucketing values, swapping rows, and pivot values.
When is CASE WHEN used?
Odd/even row swaps (exchange seats), value buckets, conditional flags, and pivot columns.
Core syntax
CASE WHEN condition THEN value [WHEN ... THEN ...] [ELSE fallback] END.Common mistakes
- Forgetting ELSE and getting NULL where a default was expected.
- Assuming conditions are evaluated lazily (they are not; order matters).
- Using CASE for a simple COALESCE or boolean filter.
Practice CASE WHEN questions
Work through them in order — each question builds on the last.