Inside a SQL SELECT statement, use CASE (CASE WHEN … THEN … ELSE … END)—not procedural IF … ELSE statements—to choose output values conditionally per row. In SQL Server (T-SQL), procedural IF … ELSE controls batch or statement flow rather than projecting per-row values in a SELECT list, whereas CASE (along with helpers like IIF() or COALESCE()) evaluates row-by-row inside expressions.
- Why CASE Works in SELECT and IF Does Not CASE (Expression): Evaluates per row inside SELECT, WHERE, GROUP BY, HAVING, and ORDER BY. Every branch returns a value expression, and the whole CASE yields one scalar value per row.
IF … ELSE (Control-of-Flow Statement): Controls which T-SQL statements or BEGIN … END blocks execute based on a boolean condition (for example, branching between two different SELECT queries), not producing conditional columns row-by-row inside a single SELECT list.
SQL – Control-of-flow IF: chooses which query runs at the statement level DECLARE @include_cancelled BIT = 0;
IF @include_cancelled = 1 BEGIN SELECT order_id, status, total_amount FROM dbo.orders; END ELSE BEGIN SELECT order_id, status, total_amount FROM dbo.orders WHERE status <> ‘CANCELLED’; END; 2. Conditional Output Values with CASE in SELECT Searched CASE (Recommended for Most Conditions) Use CASE WHEN <condition> THEN <value> for comparisons, ranges, NULL checks, or multi-column logic:
SQL SELECT order_id, customer_id, status, total_amount, CASE WHEN status = ‘CANCELLED’ THEN 0.00 WHEN total_amount >= 500.00 THEN total_amount * 0.90 ELSE total_amount END AS discounted_total, CASE WHEN shipped_date IS NULL AND status <> ‘CANCELLED’ THEN ‘Pending Dispatch’ ELSE status END AS display_status FROM dbo.orders; Simple CASE (Equality Against One Expression) Use simple CASE <expr> WHEN <val> THEN <result> only when comparing one expression directly to discrete values (=):
SQL SELECT order_id, payment_method, CASE payment_method WHEN ‘COD’ THEN ‘Cash on Delivery’ WHEN ‘CARD’ THEN ‘Credit / Debit Card’ WHEN ‘WALLET’ THEN ‘Digital Wallet’ ELSE ‘Other’ END AS payment_method_label FROM dbo.orders; 3. T-SQL Shorthand: IIF() and COALESCE() SQL Server also provides IIF(boolean_expr, true_value, false_value) for simple two-way checks—internally translated by SQL Server into a CASE expression—and COALESCE(expr1, expr2, …) when the condition is simply “first non-NULL value”:
SQL SELECT order_id, IIF(is_express = 1, ‘Express’, ‘Standard’) AS shipping_speed, COALESCE(delivery_notes, special_instructions, ‘None’) AS final_note FROM dbo.orders; Prefer explicit CASE WHEN … THEN … ELSE … END in production reference SQL when conditions grow beyond a simple binary flag or when cross-database SQL clarity matters.
- Operational Checklist Use CASE for per-row column values in SELECT; reserve IF … ELSE for statement/batch control flow.
Keep THEN and ELSE data types aligned (such as DECIMAL(18, 2) or VARCHAR(50)) so SQL Server’s data-type precedence rules do not unexpectedly truncate or coerce values.
Always define an ELSE branch when a fallback value is intended, since omitting ELSE defaults to NULL.
Use IS NULL / IS NOT NULL rather than = NULL inside WHEN conditions.
