DevLearningTools

LEARN · SQL

SQL CASE

CASE is SQL's conditional expression, if/else logic usable inside SELECT, WHERE, or ORDER BY. Learn its searched and simple forms, with portable syntax everywhere.

Every value computed so far has come straight from a column, a function, or a fixed literal. CASE is SQL's conditional expression, essentially if/else logic, usable anywhere a value is expected: SELECT, WHERE, ORDER BY, even inside an aggregate function.

Learning Objectives

  • Write a searched CASE expression with multiple WHEN conditions.
  • Use CASE inside SELECT to compute a labeled value per row.
  • Recognize the simple CASE form as a shorthand for equality checks.

A Row's Value

e.g. salary

WHEN Conditions

Checked top to bottom

First Match Wins

Or ELSE, if nothing matched

Searched CASE

Conditions are checked top to bottom, the first WHEN that's true wins, the rest are never even evaluated. ELSE is what's returned when none of the WHEN conditions match, and is optional, omitting it returns NULL for unmatched rows instead.

MySQL / MariaDB / PostgreSQL / SQLite / SQL Server / Oracle
SELECT first_name, salary,
  CASE
    WHEN salary >= 95000 THEN 'High'
    WHEN salary >= 85000 THEN 'Mid'
    ELSE 'Entry'
  END AS salary_band
FROM employees;

Simple CASE: Shorthand for Equality

The simple form, CASE column WHEN value THEN ..., only tests equality against one column, shorter to write, but strictly less flexible. The searched form, CASE WHEN condition THEN ..., can test anything in each WHEN, ranges, multiple columns, functions, which is why most of this lesson uses it.

These two are equivalent
-- Simple CASE
SELECT name,
  CASE name
    WHEN 'Engineering' THEN 'Tech'
    WHEN 'Research' THEN 'Tech'
    ELSE 'Non-Tech'
  END AS category
FROM departments;

-- Searched CASE, same result
SELECT name,
  CASE
    WHEN name = 'Engineering' THEN 'Tech'
    WHEN name = 'Research' THEN 'Tech'
    ELSE 'Non-Tech'
  END AS category
FROM departments;

CASE Inside an Aggregate

A genuinely common pattern: CASE turns a condition into a 1-or-0 per row, then SUM() adds those up, counting how many rows satisfy a condition without needing a separate query or a GROUP BY. This technique has a name, conditional aggregation, worth recognizing when it shows up in someone else's query.

MySQL / MariaDB / PostgreSQL / SQLite / SQL Server / Oracle
SELECT
  SUM(CASE WHEN salary >= 95000 THEN 1 ELSE 0 END) AS high_earners,
  COUNT(*) AS total_employees
FROM employees;

Try It Yourself

Write a query labeling each employee as 'Senior' if hired before 2020, otherwise 'Recent'.

Answer
SELECT first_name, hire_date,
  CASE
    WHEN hire_date < '2020-01-01' THEN 'Senior'
    ELSE 'Recent'
  END AS tenure
FROM employees;

Frequently Asked Questions

Can CASE be used inside ORDER BY?

Yes, a common trick for custom sort orders that don't match alphabetical or numeric order, like sorting salary_band as High, Mid, Entry instead of whatever order those strings happen to sort alphabetically.

What happens if a row matches no WHEN and there's no ELSE?

The CASE expression returns NULL for that row, the same as any expression that doesn't resolve to a value. Adding an explicit ELSE is usually clearer than relying on this implicit behavior.

Common Beginner Mistakes

Forgetting that WHEN conditions are checked in order, with the first match winning

A row matching both the first and second WHEN only ever gets the first one's result. Order conditions from most specific to least specific, or from highest threshold to lowest, as in the salary_band example above.

Omitting ELSE and being surprised by NULL results

Without ELSE, any row matching no WHEN condition silently becomes NULL rather than erroring, which can be easy to miss. Include ELSE explicitly whenever every row genuinely needs a value.

Best Practices

  • Use the searched form (CASE WHEN condition) by default, it handles everything the simple form does and more.
  • Always include ELSE unless NULL for unmatched rows is genuinely the intended behavior.
  • Order WHEN conditions deliberately, since only the first match applies.

Interview Questions

What's the difference between the simple and searched forms of CASE?

The simple form, CASE column WHEN value THEN ..., only tests one column for equality against each value. The searched form, CASE WHEN condition THEN ..., can test any condition at all in each WHEN, ranges, multiple columns, functions, making it strictly more flexible.

Write a query that counts how many employees earn over 90,000, using CASE and SUM() instead of a separate COUNT query with WHERE.

SELECT SUM(CASE WHEN salary > 90000 THEN 1 ELSE 0 END) AS high_earner_count FROM employees; — this is conditional aggregation: CASE turns the condition into 1 or 0 per row, and SUM() totals it, useful when several different conditional counts are needed side by side in one query.

Summary

CASE is SQL's conditional expression, usable anywhere a value is expected. The searched form (CASE WHEN condition) handles any condition; the simple form (CASE column WHEN value) is a shorthand limited to equality on one column. Combined with SUM(), it enables conditional aggregation.

What's Next?

NULL functions next, COALESCE, IFNULL, and ISNULL, for substituting a fallback value whenever a column turns out to be NULL.

</>

Ready to Practice SQL?

Write and run real SQL queries online, right in your browser, no install or signup required. Pick a playground based on which database you want to practice on.

SQLite / PostgreSQL

Fast, no server, no ads.

Open DB Fiddle ↗

MySQL / SQL Server

Switch databases with one dropdown.

Open SQL Fiddle ↗

Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.