SQL CASE is a conditional expression that lets you return different values based on Boolean tests inside a SELECT, UPDATE, or ORDER BY clause. It works like an if-then-else block: when a condition is true, CASE outputs the matching result; if no condition matches, the optional ELSE value is returned.
SQL CASE is a conditional expression that evaluates rows and returns a value based on the first satisfied condition, offering if-then-else logic directly inside SQL queries.
SQL CASE lets you categorize, transform, or flag data without changing the table. By embedding conditions in the query, you avoid extra joins, subqueries, or post-processing in application code.
CASE evaluates conditions in order. When a condition is true, the corresponding result is returned, and the evaluation stops. If no condition matches, CASE outputs the optional ELSE value or NULL.
SQL supports two syntaxes. Simple CASE compares one expression to several values. Searched CASE evaluates independent Boolean expressions.
-- simple case
CASE gender
WHEN 'M' THEN 'Male'
WHEN 'F' THEN 'Female'
ELSE 'Other'
END
-- searched case
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
ELSE 'C'
END
CASE can appear in SELECT lists, ORDER BY, GROUP BY, HAVING, WHERE (in some engines), UPDATE SET, and even inside window functions, making it a versatile tool.
Use Simple CASE when comparing the same column to multiple constants. It produces cleaner code and can be optimized by the query planner.
Searched CASE is ideal for range checks, compound conditions, or cross-column logic because each WHEN can reference any expression.
Add an ELSE clause at the end of the CASE. It catches all rows that fail previous conditions, ensuring no NULLs leak into the result unless desired.
Yes, you can nest CASE inside another CASE to create multi-level logic. Keep each level readable and consider common subexpressions for maintainability.
CASE in ORDER BY lets you sort rows conditionally, such as pushing NULLs to the end or prioritizing VIP customers.
SELECT *
FROM users
ORDER BY
CASE WHEN vip = true THEN 0 ELSE 1 END,
created_at DESC;
Place CASE in an UPDATE SET clause to assign different values per row. This avoids multiple UPDATE statements.
UPDATE employees
SET bonus = CASE
WHEN years_at_company >= 5 THEN salary * 0.10
WHEN years_at_company >= 2 THEN salary * 0.05
ELSE 0
END;
Yes. Wrap Boolean conditions in CASE to convert them to numeric flags, then feed them to SUM or AVG for conditional aggregation.
SELECT
SUM(CASE WHEN status = 'shipped' THEN 1 ELSE 0 END) AS shipped_orders,
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) AS pending_orders
FROM orders;
ANSI SQL defines CASE, and most engines support identical basics. Minor differences include whether NULL comparisons need IS NULL and WHERE support.
CASE is computed per row. Ensure indexes support any columns referenced in WHEN predicates. Avoid heavy scalar functions inside CASE that hinder index use.
Isolate each WHEN predicate in a SELECT to test it. Add labels like 'rule hit 1' to trace which branch fires. Comment liberally and use indentation.
Keep conditions mutually exclusive, order them by specificity, always include ELSE, and limit nesting to maintain readability.
SQL CASE adds if-then-else logic right in your query. Master both simple and searched forms, place it in any clause, and follow best practices to avoid confusion and performance issues.
Yes. Once a WHEN condition evaluates to true, CASE returns its result and ignores remaining WHEN clauses, improving efficiency.
Usually. Combining logic in one query reduces round-trips and can leverage indexes, making CASE more efficient than multiple separate statements.
Most databases disallow CASE directly in JOIN ... ON, but you can wrap the logic in a derived table or move it to the SELECT list.
SQL standards set no hard limit, but database engines impose practical limits—often hundreds. Keep the list maintainable.