SQL CASE Statement Generator

Build SQL CASE WHEN THEN ELSE END expressions visually. Choose between Simple CASE and Searched CASE, add conditions, and generate clean SQL. All processing happens in your browser.

📐 CASE Builder

WHEN
THEN
WHEN clauses: 0 Lines: 0 Characters: 0

How to Use the SQL CASE Statement Generator

Simple CASE

Use Simple CASE when you want to compare a single expression against multiple values. You provide an expression (e.g. a column name) after CASE, and each WHEN clause specifies a value to compare against it.

CASE status
  WHEN 'active' THEN 'Enabled'
  WHEN 'inactive' THEN 'Disabled'
  ELSE 'Unknown'
END

Searched CASE

Use Searched CASE when you need Boolean conditions. Each WHEN clause contains a full condition (comparisons, logical operators, etc.). This is more flexible than Simple CASE.

CASE
  WHEN salary >= 100000 THEN 'High'
  WHEN salary >= 50000 THEN 'Medium'
  ELSE 'Low'
END

Best Practices

Searched CASE evaluates conditions in order. The first matching WHEN wins, so put the most specific conditions first. Use ELSE NULL explicitly if no default value makes sense, though the ELSE clause is optional.

Common Use Cases

CASE expressions are used in SELECT lists, ORDER BY clauses, UPDATE statements, and HAVING clauses. They can also be nested for complex conditional logic.

SELECT
  name,
  CASE
    WHEN age < 18 THEN 'Minor'
    WHEN age BETWEEN 18 AND 65 THEN 'Adult'
    ELSE 'Senior'
  END as age_group
FROM users;

What a SQL CASE Expression Actually Is

CASE is an expression, not a control-flow statement. It returns one scalar value, so it can sit wherever a value is allowed: a SELECT list, WHERE, ORDER BY, GROUP BY, HAVING, the right-hand side of SET in an UPDATE, and even inside a CHECK constraint. That is the difference from procedural IF ... THEN, which decides which block of statements runs rather than producing a result you can select.

There are two forms. A Simple CASE names one expression up front and compares it for equality against each WHEN value:

CASE status WHEN 'active' THEN 'Enabled' WHEN 'inactive' THEN 'Disabled' ELSE 'Unknown' END

A Searched CASE gives each WHEN its own full predicate, so it can express ranges, IN lists, IS NULL tests and combined conditions:

CASE WHEN salary >= 100000 THEN 'High' WHEN salary >= 50000 THEN 'Medium' ELSE 'Low' END

Both evaluate top to bottom. The first matching WHEN wins and the remaining branches are skipped, so the order you write them in is part of the logic rather than formatting.

How to Use the CASE Generator

The builder is a thin layer over that syntax: it collects the parts and prints the finished expression. Nothing you type leaves the browser, and the output is plain text you paste straight into a query.

  1. Choose Simple CASE or Searched CASE in the mode toggle. Switching modes rewrites the WHEN prompt and shows or hides the expression field.
  2. For Simple CASE, type the expression being compared — usually a column such as status, category or type. Searched CASE has no expression field, because each branch carries its own condition.
  3. Fill the WHEN and THEN columns and press + Add WHEN / THEN for every extra branch. Only rows with both halves filled in are compiled, so a half-finished row is ignored instead of producing broken SQL.
  4. Set the ELSE value with Include ELSE still checked. Clear the checkbox if you deliberately want no default branch — the generated statement then ends at the last THEN and any unmatched row yields NULL.
  5. Read the result in the output panel and hit Copy. The WHEN / line / character counters refresh as you type, and Ctrl+Enter regenerates on demand. Clear resets the form back to one empty builder.

Five presets — status labels, salary bands, priority levels, age groups and rating tiers — load a complete working example so you can see the shape before writing your own mapping.

Where CASE Earns Its Keep

Copyable Examples

Labels for an enum column, built with Simple CASE. Note the ELSE: without it, any status added to the table later would silently come back as NULL.

SELECT order_id, CASE status WHEN 'paid' THEN 'Paid' WHEN 'shipped' THEN 'Shipped' WHEN 'refunded' THEN 'Refunded' ELSE 'Open' END AS status_label FROM orders;

Sorting by priority in the order a support queue is worked, using Searched CASE in ORDER BY:

SELECT id, title, priority FROM tickets ORDER BY CASE WHEN priority = 'critical' THEN 1 WHEN priority = 'high' THEN 2 WHEN priority = 'normal' THEN 3 ELSE 4 END, created_at;

Pivoting paid and unpaid totals per customer — CASE nested inside an aggregate is the portable alternative to PIVOT:

SELECT customer_id, SUM(CASE WHEN paid = 1 THEN amount ELSE 0 END) AS paid_total, SUM(CASE WHEN paid = 0 THEN amount ELSE 0 END) AS unpaid_total FROM invoices GROUP BY customer_id;

CASE vs IF() and Similar Functions

MySQL has IF(cond, a, b) and SQL Server has IIF(cond, a, b). Both are two-branch shorthands and neither is portable — the same query that runs on MySQL fails on PostgreSQL or Oracle. CASE is the ANSI spelling and works everywhere, so a query that might move between engines should standardise on it. CASE is also the only one of the three that is an expression in its own right, usable as a sort key in ORDER BY.

For a plain two-way branch, CASE WHEN x IS NULL THEN y ELSE x END and COALESCE(x, y) do the same job; COALESCE is shorter and states the intent more clearly, so prefer it there. Reach for CASE the moment you need more than two branches, a range test, or a condition that is not a simple null check. Related functions worth knowing so you do not rebuild them by hand: NULLIF(a, b) returns NULL when the two arguments are equal, and COALESCE takes any number of arguments and returns the first non-NULL one.

Common Pitfalls

No ELSE means NULL, not the original value

If nothing matches and there is no ELSE, the expression returns NULL. That NULL then propagates through anything downstream — amount * 1.1 becomes NULL, not amount, and a concatenation quietly disappears. When an unmapped value should keep its raw form, say so: ELSE status END. At minimum, map every known code and check what the unmapped ones produce.

Every THEN branch must have a compatible type

PostgreSQL is strict here and will reject a CASE whose branches cannot be resolved to one type, raising an error such as CASE types integer and text cannot be matched when you mix 1 with 'one'. Decide on the output type up front and cast the odd branch (1::text) instead of mixing.

NULL never equals NULL in a Simple CASE

CASE col WHEN NULL THEN ... does not match rows where col is NULL, because comparing anything to NULL yields unknown rather than true. Handle nulls with a Searched CASE and an explicit WHEN col IS NULL branch.

Order decides the winner

Conditions are tested in sequence and the first true one stops evaluation. Overlapping ranges make later branches unreachable — WHEN score >= 50 written before WHEN score >= 90 means the second never fires for any value, since 95 matches the first test first. Put the narrowest or highest-priority condition at the top.

Stop nesting at two levels

A CASE inside a CASE inside a CASE is hard to review and usually a sign that the mapping belongs in a lookup table instead. If a mapping is stable and shared across queries, build a reference table and join to it — the query gets shorter and the values become editable without a deploy.

Related Tools

SQL Formatter· SQL Validator· Data Generator· SQL Escape

Related Guides

In-depth explainers for the problems this tool solves:

Generate Test Data with SQL