Skip to main content
Log inGet a demo
Basic statements and clauses

SQL CASE

What is the SQL CASE Function?

The SQL CASE function is a powerful and versatile conditional expression that allows you to perform conditional logic within SQL queries. It is used to evaluate multiple conditions and return different values or perform different actions based on the results of these conditions. Essentially, it's a way to create conditional statements within your SQL queries.

When You Would Use the SQL CASE Function

You would use the SQL CASE function when you need to perform different operations or return different values based on specified conditions in your SQL queries. Common use cases include:

  1. Data Transformation: Transforming data values based on certain conditions, like categorizing data into different groups or ranges.

  2. Data Filtering: Filtering rows based on specific conditions, such as excluding or including certain records in the result set.

  3. Custom Calculations: Calculating custom values or aggregations based on multiple conditions.

  4. Ordering: Changing the order in which data is displayed in the result set.

Syntax

The basic syntax for the SQL CASE function is as follows:

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    ...
    [ELSE else_result]
END
  • condition1, condition2, etc.: The conditions to be evaluated.
  • result1, result2, etc.: The values or expressions to return when the corresponding condition is true.
  • else_result (optional): The value to return when none of the conditions are met.

The CASE statement can be used in both the SELECT and UPDATE statements.

Parameter Values

  • condition1, condition2, etc.: These are conditions that you want to evaluate. These can be simple conditions, expressions, or even subqueries.
  • result1, result2, etc.: The values or expressions to return when the corresponding condition is true. These can be literals, column values, or expressions.
  • else_result (optional): The value to return when none of the conditions are met. It is also possible to omit the ELSE clause.

Example Query

Consider a simple example where you want to categorize employees based on their salary into three categories: 'Low', 'Medium', and 'High'. The SQL query would look like this:

SELECT
    EmployeeName,
    Salary,
    CASE
        WHEN Salary < 30000 THEN 'Low'
        WHEN Salary < 60000 THEN 'Medium'
        ELSE 'High'
    END AS SalaryCategory
FROM Employees;

Example Table Response

The result set of the above query might look like this:

EmployeeNameSalarySalaryCategory
John25000Low
Alice45000Medium
Bob75000High

Use Cases

The SQL CASE function is commonly used in various scenarios, including:

  1. Custom Report Generation: Create custom reports by formatting data differently based on conditions.
  2. Data Cleaning: Standardize or clean data by replacing or categorizing values.
  3. Security and Permissions: Control access to data based on user roles or permissions.
  4. Dynamic Sorting: Sort data in custom orders based on specific criteria.
  5. Data Aggregation: Perform complex aggregations with different rules for different groups.

SQL Languages

The SQL CASE function is widely supported and available in many SQL database systems, including but not limited to:

  • MySQL
  • PostgreSQL
  • SQL Server (Transact-SQL)
  • Oracle Database (PL/SQL)
  • SQLite
  • IBM Db2
  • Teradata

The specific syntax and behavior may vary slightly between database systems, but the fundamental concept of conditional logic remains consistent. Always consult the documentation of your specific database system for exact details on its usage.


Keep reading

SQL ORDER BY

Read article

SQL GROUP BY

Read article

SQL HAVING

Read article

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.