Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Write a CASE Statement in PostgreSQL
The CASE statement in PostgreSQL lets you add conditional logic to your SQL queries, making them more dynamic and powerful. In this guide, we’ll walk you through how to write and use CASE statements with clear examples.
What is a CASE Statement?
A CASE statement works like an IF-THEN-ELSE block inside your SQL query. It allows you to return specific values based on conditions you define, all within a single query.
Basic Syntax
SELECT 
    column1,
    CASE 
        WHEN condition1 THEN result1
        WHEN condition2 THEN result2
        ELSE result_default
    END AS new_column
FROM table_name;
                                Example: Categorize Orders by Amount
SELECT 
    order_id,
    amount,
    CASE 
        WHEN amount < 100 THEN 'Small'
        WHEN amount BETWEEN 100 AND 500 THEN 'Medium'
        ELSE 'Large'
    END AS order_category
FROM orders;
                                This query will add an order_category column that classifies orders as Small, Medium, or Large based on their amount.
Using CASE in WHERE Clauses
While you can’t directly put a CASE in the WHERE clause, you can use it inside the conditions:
SELECT *
FROM orders
WHERE 
    (CASE 
        WHEN customer_status = 'VIP' THEN amount > 500
        ELSE amount > 1000
    END);
                                Best Practices
- Keep conditions clear and avoid overlapping ranges.
- Use ELSEto handle unexpected cases.
- Test your CASElogic with small datasets before deploying.
Summary
The PostgreSQL CASE statement is a versatile tool for adding conditional logic to your queries. Whether you’re categorizing data, transforming values, or customizing output, mastering CASE will help you write smarter, more efficient SQL.
CASE with aggregate functions like SUM() or COUNT() to perform conditional aggregations.