The SQL CASE statement lets you return different values in the same column based on conditional logic—similar to IF-THEN-ELSE in programming. Inside SELECT, UPDATE, ORDER BY, and HAVING clauses, CASE evaluates each condition in order and returns the first matching result, or an optional ELSE value. This enables readable, set-based conditional transformations without multiple queries or JOINs.
SQL CASE returns a value based on the first true condition, enabling inline conditional logic across SELECT, UPDATE, ORDER BY, and HAVING clauses.
SQL CASE is an expression that checks one or more Boolean conditions and returns the corresponding result. It works like IF-THEN-ELSE, but inside a single query.
CASE keeps logic set-based, avoiding procedural loops and UNION hacks. You transform or bucket rows in one pass, which is faster and easier to maintain.
CASE can sit in SELECT lists, WHERE filters, ORDER BY sort keys, GROUP BY or HAVING aggregations, and UPDATE/DELETE SET clauses, making it a versatile tool.
The database evaluates WHEN clauses top-to-bottom. It stops at the first true condition and returns its THEN value; otherwise, it yields the optional ELSE or NULL.
Simple CASE compares one expression to multiple values.
CASE status
WHEN 'P' THEN 'Pending'
WHEN 'S' THEN 'Shipped'
ELSE 'Unknown'
END
Searched CASE allows full Boolean expressions.
CASE
WHEN total >= 1000 THEN 'High'
WHEN total >= 500 THEN 'Medium'
ELSE 'Low'
END
Yes; CASE chains conditions cleanly, reducing deeply nested IFs and making SQL easier to read and debug.
Write multiple CASE expressions, each producing a column for a category, to pivot rows into flags or counts without a dedicated PIVOT clause.
Wrapping an aggregate argument in CASE lets you sum or count only rows meeting chosen conditions in one SELECT, saving extra subqueries.
Always include ELSE to make unexpected states explicit. It prevents silent NULLs and surfaces edge cases for easier debugging.
CASE runs per row and is fast when conditions are simple. Complex functions inside WHEN may prevent index use, so keep predicates sargable when possible.
ANSI SQL defines CASE, so syntax is consistent across PostgreSQL, MySQL, SQL Server, Snowflake, BigQuery, and Oracle, with minor whitespace variations.
Place a secondary CASE inside a THEN or ELSE. Keep depth minimal; deep nesting hurts readability and invites logic bugs.
Front-load most selective conditions, add ELSE, use searched CASE for complex predicates, and keep code readable with indentation.
SQL CASE provides inline conditional logic, executes top-to-bottom, and works in all major clauses. Use it to transform, bucket, and aggregate data without procedural code.
Yes. Wrap aggregate logic in a subquery or GROUP BY query, then place CASE around the aggregate result.
The ANSI standard sets no hard limit; practical limits depend on database engine memory, but dozens are common.
Absolutely. CASE pairs with OVER() clauses to build conditional windows or flags inside analytic calculations.
Add one WHEN at a time, test with SELECT, and include an ELSE 'Debug' to catch unhandled rows.