SQL > Advanced SQL > Case

CASE is used to provide if-then-else type of logic to SQL. There are two formats: The first is a Simple CASE expression, where we compare an expression to static values. The second is a Searched CASE expression, where we compare an expression to one or more logical conditions.

Key Takeaway: CASE brings conditional logic into SQL queries without needing to process data in application code. Use it to categorize values, apply different calculations, and pivot data all within a single query.

Simple CASE Expression Syntax

The syntax for a simple CASE expression is:

SELECT CASE ("column_name")
  WHEN "value1" THEN "result1"
  WHEN "value2" THEN "result2"
  ...
  [ELSE "resultN"]
  END
FROM "table_name";

The ELSE clause is optional.

Simple CASE Expression Example

We use the following table for our example.

Table Store_Information

 Store_Name  Sales  Txn_Date 
 Los Angeles 1500  Jan-05-1999 
 San Diego 250  Jan-07-1999 
 San Francisco 300  Jan-08-1999 
 Boston 700  Jan-08-1999 

To multiply the sales amount from 'Los Angeles' by 2 and the sales amount from 'San Diego' by 1.5 while keeping the sales amount for other stores the same, we would use the following SQL statement using CASE:

SELECT Store_Name, CASE Store_Name
  WHEN 'Los Angeles' THEN Sales * 2
  WHEN 'San Diego' THEN Sales * 1.5
  ELSE Sales
  END
"New Sales",
Txn_Date
FROM Store_Information;

"New Sales" is the name given to the column with the CASE statement. This is an example of a simple CASE expression, because the conditions listed, 'Los Angeles' and 'San Diego', are static values.

Result:

Store_Name New Sales Txn_Date
Los Angeles 3000 Jan-05-1999
San Diego 375 Jan-07-1999
San Francisco 300 Jan-08-1999
Boston 700 Jan-08-1999

Searched CASE Expression Syntax

The syntax for a searched CASE expression is:

SELECT CASE
  WHEN "condition1" THEN "result1"
  WHEN "condition2" THEN "result2"
  ...
  [ELSE "resultN"]
  END
FROM "table_name";

The ELSE clause is optional. "Condition" can consist of one or more logical statements.

Searched CASE Expression Example

We use the same Store_Information above. If we want to define the status of a store's sale based on the following rules:

  • If Sales >= 1,000, it's a "Good Day"
  • If Sales >= 500 and < 1,000, it's an "OK Day"
  • If Sales < 500, it's a "Bad Day"

    We can use the following searched CASE expression:

    SELECT Store_Name, Txn_Date, CASE
      WHEN Sales >= 1000 THEN 'Good Day'
      WHEN Sales >= 500 THEN 'OK Day'
      ELSE 'Bad Day'
      END
    "Sales Status"
    FROM Store_Information;

    Result:

    Store_Name Txn_Date Sales Status
    Los Angeles Jan-05-1999 Good Day
    San Diego Jan-07-1999 Bad Day
    San Francisco Jan-08-1999 Bad Day
    Boston Jan-08-1999 OK Day

    Note that a simple CASE expression is a special case of a searched CASE expression. As an example, the following two CASE expressions are identical:

    Simple CASE Expression:

    SELECT Store_Name, CASE Store_Name
      WHEN 'Los Angeles' THEN Sales * 2
      WHEN 'San Diego' THEN Sales * 1.5
      ELSE Sales
      END
    "New Sales",
    Txn_Date
    FROM Store_Information;

    Searched CASE Expression:

    SELECT Store_Name, CASE
      WHEN Store_Name = 'Los Angeles' THEN Sales * 2
      WHEN Store_Name = 'San Diego' THEN Sales * 1.5
      ELSE Sales
      END
    "New Sales",
    Txn_Date
    FROM Store_Information;

    Frequently Asked Questions

    Can I use CASE in a WHERE clause?

    Yes. CASE can appear in WHERE to create complex conditional filters. However, it is more common to see CASE in the SELECT list. In WHERE, you would typically use: WHERE CASE WHEN ... THEN ... END = 'value'.

    Can I use CASE in GROUP BY or ORDER BY?

    Yes. Using CASE in ORDER BY is common for custom sort logic. For example: ORDER BY CASE WHEN status = 'urgent' THEN 1 ELSE 2 END puts urgent items first.

    What happens if no WHEN condition matches and there is no ELSE?

    The CASE expression returns NULL. Always include ELSE to handle unexpected values and avoid silent NULLs in your result set.

    Can CASE be used inside aggregate functions?

    Yes. This is a powerful pattern: SUM(CASE WHEN status = 'A' THEN amount ELSE 0 END) sums only rows where status is 'A'. This is commonly used for conditional aggregation / pivot tables.



    Next: SQL Pivot

    This page was last updated on March 19, 2026.




    Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact