Zero Signup ToolsFree browser tools

Developer Tools

SQL CASE WHEN Builder

Build SQL CASE WHEN expressions with multiple branches, ELSE, and aliases. Supports Postgres, MySQL, SQL Server, SQLite, Oracle, BigQuery, and ANSI SQL.

CASE options

Choose the SQL dialect, pick simple or searched CASE, and set the alias and surrounding columns for the full SELECT preview.

CASE form

WHEN takes a full predicate. Best for ranges and multi-column conditions.

Output style

Common patterns

Tap a preset to load a worked CASE example. You can still tweak any field after.

WHEN / THEN branches

Each branch becomes one WHEN line. Branches are evaluated in order, so put the most specific rules first.

WHEN branch 1
WHEN branch 2
WHEN branch 3
Up to 20 branches. SQL evaluates them top to bottom.

CASE expression

CASE
  WHEN score >= 9 THEN 'promoter'
  WHEN score >= 7 THEN 'passive'
  WHEN score >= 0 THEN 'detractor'
  ELSE 'unknown'
END AS segment

Drop this straight into a SELECT list, an UPDATE SET, an ORDER BY, or a GROUP BY.

Full SELECT preview

SELECT
  id, email,
  CASE
    WHEN score >= 9 THEN 'promoter'
    WHEN score >= 7 THEN 'passive'
    WHEN score >= 0 THEN 'detractor'
    ELSE 'unknown'
  END AS segment
FROM responses;

Paste into your SQL console to confirm the column shows up exactly as designed.

Conditional aggregation (count or sum matching rows)

SUM(CASE WHEN score >= 9 THEN 1 ELSE 0 END) AS segment

The SUM(CASE WHEN ... THEN 1 ELSE 0 END) idiom counts rows matching the first WHEN branch. Swap 1 for a column to total amounts instead.

How CASE differs across dialects

  • Standard / ANSI SQL: CASE is part of the core spec. Both forms (simple and searched) work anywhere. END closes the expression; no END CASE for inline use.
  • PostgreSQL: Identical to standard CASE. Postgres also has a FILTER clause on aggregates (COUNT(*) FILTER (WHERE ...)) that is cleaner than SUM(CASE WHEN ... THEN 1 ELSE 0 END) for counting matches.
  • MySQL / MariaDB: CASE supports both forms. MySQL also has an IF(cond, a, b) shorthand for two-branch logic, but CASE is the portable choice.
  • SQLite: Standard CASE. Booleans are stored as integers (0 / 1), so boolean kinds emit 1 or 0 instead of TRUE or FALSE.
  • SQL Server (T-SQL): CASE is an expression, not a statement. There is no END CASE; just END. T-SQL also exposes IIF(cond, a, b) for two-branch logic.
  • Oracle: CASE works as in standard SQL. Date literals are emitted with TO_DATE so they parse without an implicit format conversion.
  • BigQuery: Standard CASE. BigQuery also has IF(cond, a, b) and a typed DATE / TIMESTAMP literal prefix that this tool emits for date kinds.

How to use

  1. Pick the SQL dialect that matches your database and choose Searched CASE (predicates per WHEN, best for ranges and multi-column tests) or Simple CASE (literal values compared against the expression you type in the CASE expression field).
  2. Tap a Common pattern preset like NPS bucket, Age groups, Subscription status label, or Conditional flag to load a worked example, then edit any field to match your schema.
  3. Add WHEN branches and fill in the predicate (Searched) or value plus kind (Simple) for each one, plus the THEN result and its kind. Use the Up and Down buttons to control evaluation order: the first matching WHEN wins, so most specific first.
  4. Optionally tick ELSE clause to set a fallback value (without it, unmatched rows resolve to NULL). Set the alias, table, schema, and other SELECT columns to shape the full SELECT preview around your CASE column.
  5. Copy the bare CASE expression for a SELECT list or ORDER BY, the full SELECT statement for your SQL console, or the SUM(CASE WHEN ... THEN 1 ELSE 0 END) conditional aggregation example to count matching rows.

About this tool

SQL CASE WHEN Builder composes a SQL CASE expression from a small form so you can stop hand-typing brittle conditional columns. Pick the dialect (PostgreSQL, MySQL/MariaDB, SQLite, SQL Server, Oracle, BigQuery, or generic ANSI SQL) and the form of CASE you want: searched CASE, where each WHEN takes a full predicate like score >= 9 or status IN ('paused', 'cancelled'), or simple CASE, where each WHEN takes a literal value compared against an expression you put after CASE (status, LOWER(plan), or any computed value). Add as many WHEN branches as you need (up to twenty), drag them up and down to control evaluation order (CASE matches the first WHEN that succeeds, so the most specific rules belong first), and pick a value kind per branch (string, number, boolean, NULL, date, or raw SQL) so literals get quoted, escaped, and prefixed correctly for the dialect: TRUE/FALSE on Postgres and MySQL, 1/0 on SQLite, SQL Server, and Oracle, DATE 'YYYY-MM-DD' on Postgres and BigQuery, TO_DATE() on Oracle. Tick the ELSE clause to set a fallback for rows that match no WHEN branch (without ELSE, those rows resolve to NULL, which is a real footgun in conditional aggregation), set an optional alias for the column, and pick the surrounding SELECT columns and table so the full SELECT preview shows your CASE column in context. Six built-in presets cover the patterns analysts hit constantly: NPS bucketing (promoter / passive / detractor), age groups with NULL handling, subscription status labels, conditional 1/0 flag columns for SUM aggregation, NULL coalescing with a literal fallback, and order total bands. The output panel shows the bare CASE expression on its own (drop it straight into a SELECT, GROUP BY, ORDER BY, or UPDATE SET), the full SELECT statement with your table and columns, and a conditional aggregation example (SUM(CASE WHEN ... THEN 1 ELSE 0 END) AS alias) you can use to count or sum matching rows in a single pass. Validation flags the common mistakes: empty WHEN predicates, missing THEN results, duplicate WHEN values in simple CASE (unreachable after the first match), and missing ELSE clauses. Everything runs in your browser; the column names, table names, predicates, and literal values you type here never leave your device.

Free to use. Works in your browser. No signup, no login.

Related tools

You may also like

All tools
All toolsDeveloper Tools