SQL CASE Statement: Conditional Logic in Your Queries

Published: 2026-02-11

The CASE statement is SQL’s way of adding conditional logic to your queries. It works like an if-then-else statement, allowing you to return different values based on conditions. CASE is supported in all major SQL databases including SQLite, PostgreSQL, MySQL, SQL Server, and Oracle.

Sample Data

We use two tables in all examples:

products table:

id name category price stock rating
1 Laptop Pro Electronics 1299.99 15 4.5
2 Wireless Mouse Electronics 29.99 50 4.2
3 Office Chair Furniture 249.99 8 4.7
4 Desk Lamp Furniture 39.99 25 3.9
5 Notebook Pack Stationery 12.99 100 4.0
6 Pen Set Stationery 8.99 75 3.5
7 Monitor 27" Electronics 399.99 0 4.6
8 Standing Desk Furniture 599.99 3 4.8

orders table:

id product_id quantity order_date status
1 1 2 2026-02-01 delivered
2 2 5 2026-02-03 shipped
3 3 1 2026-02-05 processing
4 4 3 2026-02-06 delivered
5 5 10 2026-02-07 shipped
6 1 1 2026-02-08 cancelled
7 7 1 2026-02-09 pending
8 8 1 2026-02-10 processing

Simple CASE Expression

The simple CASE expression compares a column or expression to specific values. It’s concise when checking equality.

Syntax

CASE column_name
  WHEN value1 THEN result1
  WHEN value2 THEN result2
  ELSE default_result
END

Example: Categorize Order Status

Translate order status codes into user-friendly labels:

Searched CASE Expression

The searched CASE expression evaluates boolean conditions, allowing complex comparisons and ranges.

Syntax

CASE
  WHEN condition1 THEN result1
  WHEN condition2 THEN result2
  ELSE default_result
END

Example: Price Categories

Classify products by price range:

Example: Stock Availability Status

Determine inventory status with different thresholds:

Using CASE in Calculations

CASE statements can modify calculations, apply conditional formulas, or handle NULL values.

Example: Apply Category-Based Discounts

Calculate discounts based on product category:

Example: Priority Orders by Value and Status

Calculate order priority based on multiple factors:

CASE in Aggregations

Use CASE inside aggregate functions to create pivot-like reports or conditional counts.

Example: Count Products by Availability

Count how many products are in each stock category:

Example: Revenue by Category and Status

Calculate different metrics using conditional aggregation:

CASE in ORDER BY

Use CASE in ORDER BY to create custom sorting logic.

Example: Custom Sort Order

Sort products with custom priority (out of stock first, then by rating):

Nested CASE Statements

CASE expressions can be nested for more complex logic.

Example: Complex Product Classification

Combine multiple criteria for detailed categorization:

Common Use Cases for CASE Statements

  • Data categorization — Group values into ranges or categories
  • Conditional formatting — Display user-friendly labels or status indicators
  • Dynamic calculations — Apply different formulas based on conditions
  • Pivot table creation — Combine with aggregations for crosstab reports
  • Custom sorting — Define specific sort orders for special business logic
  • Handling NULL values — Replace or flag missing data
  • Multi-criteria decisions — Implement complex business rules in SQL

CASE vs. IIF and COALESCE

  • CASE — Most flexible, handles multiple conditions
  • IIF(condition, true_value, false_value) — Simpler syntax for single condition (SQL Server, Access)
  • COALESCE(val1, val2, …) — Returns first non-NULL value (good for NULL handling)

SQLite supports both CASE and COALESCE, but not IIF. Use CASE for maximum compatibility.

Performance Tips

  1. Order conditions wisely — Put most likely conditions first
  2. Avoid redundant evaluations — CASE is evaluated sequentially; once a condition matches, it stops
  3. Index appropriate columns — If CASE uses column comparisons, indexes can help
  4. Consider computed columns — For frequently used CASE logic, some databases support indexed computed columns

Summary

  • CASE adds conditional logic to SQL queries with if-then-else behavior
  • Simple CASE compares a column to specific values: CASE column WHEN value THEN result
  • Searched CASE evaluates boolean conditions: CASE WHEN condition THEN result
  • Can be used in SELECT, WHERE, ORDER BY, and aggregate functions
  • CASE expressions can be nested for complex decision trees
  • Always include an ELSE clause to handle unexpected values
  • Supported in all major SQL databases with consistent syntax

Try modifying the examples to experiment with different conditions and see how CASE can simplify your conditional logic!