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;

Related Tools

SQL Formatter· SQL Validator· Data Generator· SQL Escape