SQL-Engineering-Handbook

01 · Basic CASE WHEN

Difficulty: Beginner · Estimated time: 15 min Schema: 00_Sample_Schema.sql

CASE evaluation flow

Introduction

CASE WHEN is SQL’s conditional expression. It lets a query return a different value depending on data, without leaving SQL for application code. Every dashboard filter, every “status” column you’ve ever seen in a BI tool, and most feature-engineering pipelines lean on this one expression.

Learning Objectives

By the end of this lesson you will be able to:

Business Context

Raw data is rarely how business users think. A manager_id column is a database implementation detail; “Has Manager” / “No Manager” is a sentence a stakeholder can act on. Translating raw values into business language, directly in the query, is the single most common reason CASE exists in production SQL.

Syntax

Searched CASE — evaluates arbitrary boolean conditions:

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    ELSE result_default
END

Simple CASE — compares one expression against a list of exact values (shorter, but less flexible — no ranges, no IS NULL, no compound conditions):

CASE column_name
    WHEN value1 THEN result1
    WHEN value2 THEN result2
    ELSE result_default
END

Syntax Breakdown

Clause Required? Notes
CASE Yes Opens the expression
WHEN ... THEN ... At least one Evaluated top to bottom
ELSE No, but strongly recommended If omitted and nothing matches, the result is NULL
END Yes Closes the expression

Visual Explanation

                 ┌────────────────────────┐
 row enters ───► │ WHEN condition1 TRUE?  │──Yes──► result1
                 └───────────┬────────────┘
                              │ No
                 ┌───────────▼────────────┐
                 │ WHEN condition2 TRUE?  │──Yes──► result2
                 └───────────┬────────────┘
                              │ No
                              ▼
                        ELSE result_default
flowchart TD
    A[Row enters CASE] --> B{condition1?}
    B -- true --> R1[Return result1]
    B -- false --> C{condition2?}
    C -- true --> R2[Return result2]
    C -- false --> D[Return ELSE value]

Engineering Notes

Production Applications

SQL

See 01_Basic_CASE_WHEN.sql for the runnable example against the shared schema.

Best Practices

Common Mistakes

Mistake Consequence
Omitting ELSE Unmatched rows return NULL, not an error — easy to miss in QA
Comparing NULL with = inside a searched CASE x = NULL is never TRUE in standard SQL; use x IS NULL
Assuming WHEN order doesn’t matter It does — first match wins, always

Edge Cases

Dialect Differences

Dialect Notes
PostgreSQL / SQL Server / Oracle / MySQL CASE ... WHEN ... END syntax is identical (ANSI SQL)
SQL Server Also offers IIF(condition, true_val, false_val) as shorthand for a two-branch CASE
BigQuery Identical CASE syntax; also supports IF(condition, true_val, false_val)
Oracle Also offers DECODE(expr, val1, res1, val2, res2, default) — older, value-equality only, no ranges

Performance Notes

CASE itself adds negligible overhead — it’s evaluated per row in memory. Performance problems arise when a CASE expression wraps an indexed column inside a WHERE clause, since that usually prevents index usage. Keep CASE in the SELECT list for derived columns; keep raw column comparisons in WHERE where possible.

Interview Questions

  1. What’s the difference between simple and searched CASE?
  2. What does CASE return when no WHEN matches and there’s no ELSE?
  3. Does CASE evaluate all branches or stop at the first match?
  4. Why does WHEN x = NULL never match?
Answers 1. Simple CASE compares one expression against exact values only; searched CASE evaluates arbitrary boolean conditions (ranges, `IS NULL`, `AND`/`OR`). 2. `NULL`. 3. It stops at the first `TRUE` condition (short-circuit, top to bottom). 4. Because `NULL` represents "unknown," and any equality comparison against an unknown value is itself unknown (`NULL`), never `TRUE`. Use `IS NULL`.

Summary

CASE WHEN is SQL’s inline conditional expression — evaluated top to bottom, first match wins, always include ELSE. It’s the foundation every later lesson in this module builds on.

Further Reading / Cross References