Back to Library

CASE in SQL

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Syntax

-- Simple CASE
CASE expression
    WHEN value1 THEN result1
    WHEN value2 THEN result2
    [ELSE else_result]
END

-- Searched CASE
CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    [ELSE else_result]
END
Return type
Depends on result expressions (must be compatible types)

CASE Function Example

SELECT product_name,
       CASE WHEN price > 100 THEN 'High'
            WHEN price > 50 THEN 'Medium'
            ELSE 'Low'
       END AS price_category
FROM products;
-- Output: 'Product A', 'High'

What is CASE in SQL?

The CASE statement in SQL allows you to implement conditional logic within queries. It evaluates conditions and returns a specified result based on the first matching condition. If no conditions match, an optional ELSE clause can provide a default value.

The CASE statement is useful for categorizing data, conditional calculations, and dynamic result formatting. It is supported in SQL Server, MySQL, PostgreSQL, and Oracle.

The CASE statement is a powerful tool in data transformation, reporting, and conditional processing.

Parameters:

  • expression (Simple CASE): Value to compare against WHEN clauses

  • condition (Searched CASE): Boolean conditions to evaluate

  • result: Value to return when condition is met

  • else_result (optional): Default value if no conditions match

Example Use Cases:

-- Grade calculation

SELECT student_name,
       CASE 
           WHEN score >= 90 THEN 'A'
           WHEN score >= 80 THEN 'B'
           WHEN score >= 70 THEN 'C'
           ELSE 'F'
       END AS grade
FROM exam_results;
-- Output:
student_name    | grade
----------------|-------
John Smith      | A
Maria Garcia    | A
James Brown     | B
Sarah Wilson    | B
Michael Lee     | C
Emily Davis     | C
David Martinez  | F

-- Status mapping

SELECT order_id,
       CASE status
           WHEN 'P' THEN 'Pending'
           WHEN 'S' THEN 'Shipped'
           WHEN 'D' THEN 'Delivered'
           ELSE 'Unknown'
       END AS order_status
FROM orders;
-- Output:
order_id    | order_status
------------|-------------
1001        | Pending
1002        | Shipped
1003        | Delivered
1004        | Pending
1005        | Shipped
1006        | Unknown
1007        | Delivered

-- Conditional aggregation

SELECT COUNT(*) as total_orders,
       SUM(CASE 
           WHEN status = 'Completed' THEN 1 
           ELSE 0 
       END) as completed_orders
FROM orders;
-- Output:
total_orders | completed_orders
-------------|------------------
1000         | 750

Notes:

  • Behavior: Returns NULL if no conditions match and no ELSE specified

  • Performance Considerations: Conditions evaluated in order

  • Version Info: Core SQL feature available in all DBMS

  • Deprecated/Recommended Alternatives: None, standard functionality

Error Handling:

  • Error: Type mismatch between result expressions

  • Recommendation: Ensure consistent return types


Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

CASE

CASE WHEN col > 0 THEN 'Positive' ELSE 'Zero' END

Returns ELSE value or NULL

PostgreSQL

CASE

CASE WHEN col > 0 THEN 'Positive' ELSE 'Zero' END

Returns ELSE value or NULL

SQL Server

CASE

CASE WHEN col > 0 THEN 'Positive' ELSE 'Zero' END

Returns ELSE value or NULL

SQLite

CASE

CASE WHEN col > 0 THEN 'Positive' ELSE 'Zero' END

Returns ELSE value or NULL

BigQuery

CASE

CASE WHEN col > 0 THEN 'Positive' ELSE 'Zero' END

Returns ELSE value or NULL

Snowflake

CASE

CASE WHEN col > 0 THEN 'Positive' ELSE 'Zero' END

Returns ELSE value or NULL

Athena

CASE

CASE WHEN col > 0 THEN 'Positive' ELSE 'Zero' END

Returns ELSE value or NULL

Related Functions:

CAST/CONVERT()

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

Converts a value from one data type to another. Both CAST and CONVERT perform the same function with different syntax.

LAST_VALUE()

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

Returns the last value in an ordered partition of a result set.

FIRST_VALUE()

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

Returns the first value in an ordered partition of a result set.

LEAD()

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

Accesses data from a subsequent row in the result set within a specified window partition.

LAG()

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

Accesses data from a previous row in the result set within a specified window partition.

DENSE_RANK()

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with no gaps in rank numbers when ties occur.

CASE

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

Provides conditional logic in SQL queries, evaluating conditions and returning specified results based on matching conditions.

RANK()

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

Assigns a rank to rows within a result set partition, with gaps in rank numbers when ties occur.

ROW_NUMBER()

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

Assigns a unique sequential integer to rows within a result set partition.

One place for all your queries,
directly on your SQL editor