CASE WHEN in SQL Server is a T-SQL expression that returns conditional logic within SELECT, UPDATE, or ORDER BY clauses, similar to IF-THEN-ELSE.
Master conditional logic in T-SQL with concise syntax, real examples, and best practices.
CASE WHEN is a T-SQL expression that evaluates conditions in order and returns the first matching result; otherwise it returns an optional ELSE value. It works inside SELECT, UPDATE, DELETE, ORDER BY, and HAVING clauses.
SQL Server reads CASE, checks each WHEN Boolean expression top-down, returns the THEN value of the first true condition, and exits the evaluation. If no condition is true and an ELSE is present, that value is returned; if not, NULL is returned.
Syntax: CASE WHEN <condition1> THEN <result1> WHEN <condition2> THEN <result2> ELSE <default> END
. A simple CASE variant compares one expression to many values: CASE <expr> WHEN <value1> THEN <result1> … END
.
CASE WHEN centralizes logic, reduces round trips, and keeps queries set-based. It avoids client-side IF statements and enables dynamic grouping, bucketing, or pivoting data in a single scan.
CASE WHEN can be embedded in SELECT lists to create derived columns, in WHERE/HAVING to filter conditionally, in ORDER BY for custom sorting, and in UPDATE/SET to modify rows differently based on criteria.
Use sequential WHEN clauses that check range boundaries, returning a label like ‘Small’, ‘Medium’, or ‘Large’. This pattern segments metrics for dashboards and reports.
Within a single statement, yes. CASE WHEN is an expression and cannot control flow between multiple statements, but it can output different values per row, unlike IF ELSE which runs once per batch.
Order WHEN clauses from most-specific to least-specific, keep expressions sargable when possible, cast consistent data types, and always include an ELSE branch to avoid accidental NULLs.
Galaxy’s AI Copilot autocompletes CASE blocks, suggests range labels, and warns about missing ELSE clauses. Inline previews let you verify results instantly without leaving the editor.
The query below transforms numeric scores into letter grades using CASE WHEN and returns aggregated counts per grade.SELECT
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
WHEN score >= 70 THEN 'C'
WHEN score >= 60 THEN 'D'
ELSE 'F'
END AS grade,
COUNT(*) AS students
FROM dbo.ExamResults
GROUP BY
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
WHEN score >= 70 THEN 'C'
WHEN score >= 60 THEN 'D'
ELSE 'F'
END;
CASE WHEN itself is lightweight, but non-sargable expressions (e.g., functions around indexed columns) can force scans. Use computed columns or indexed views for heavy reuse.
Comment out branches, use SELECT …, CASE WHEN … END AS debug_flag
, and leverage Galaxy’s live result diff to ensure each range maps correctly.
Conditional logic is essential for reporting, data transformation, and feature engineering. CASE WHEN lets data engineers express this logic directly in SQL Server, avoiding procedural code, keeping operations set-based, and leveraging indexes. Mastery accelerates ETL pipelines, minimizes query count, and improves maintainability across analytics workflows.
Yes, but it may prevent index usage. Prefer separate JOIN predicates or computed columns when possible.
SQL Server allows up to 255 WHEN branches in a single CASE expression, enough for most use cases.
Generally yes because it keeps logic in one set-based operation, reducing I/O and context switches.
Galaxy autocompletes CASE syntax, highlights missing ELSE clauses, and previews results, speeding up development.