🗄️ SQL examples

CASE WHEN Examples in SQL

Create conditional labels, buckets and flags. This page gives you the syntax, five practical examples, common mistakes, and copy-ready SQL you can adapt.

Updated 2026-06-125 practical examplesCopy-ready SQL

What CASE WHEN does

Create conditional labels, buckets and flags. SQL syntax can vary by database, but the pattern below is a useful starting point for reports and analysis.

Syntax or pattern

CASE WHEN condition THEN result ELSE other_result END
✍️

5 practical examples

1

Create order size buckets

Label orders as small, medium or large.

SELECT order_id, total_amount, CASE WHEN total_amount >= 1000 THEN 'Large' WHEN total_amount >= 250 THEN 'Medium' ELSE 'Small' END AS order_size FROM orders;

CASE creates readable categories for reports.

2

Flag overdue tickets

Mark tasks that are past their due date.

SELECT ticket_id, due_date, CASE WHEN due_date < CURRENT_DATE THEN 'Overdue' ELSE 'On track' END AS status_flag FROM tickets;

This turns date logic into a simple label.

3

Group customers by activity

Classify customers by order count.

SELECT customer_id, order_count, CASE WHEN order_count >= 10 THEN 'Frequent' ELSE 'Occasional' END AS customer_type FROM customer_summary;

Useful for segmentation and dashboards.

4

Conditional sales total

Sum sales only for completed orders.

SELECT SUM(CASE WHEN status = 'Completed' THEN total_amount ELSE 0 END) AS completed_sales FROM orders;

CASE inside SUM is a common reporting pattern.

5

Replace null-like labels

Create a clean label for missing assignments.

SELECT ticket_id, CASE WHEN agent_id IS NULL THEN 'Unassigned' ELSE 'Assigned' END AS assignment_status FROM tickets;

This makes operational reports easier to scan.

Common mistakes to avoid

  • Forgetting that SQL dialects vary across PostgreSQL, SQL Server, MySQL, BigQuery and SQLite.
  • Using SELECT * in production reports when only a few columns are needed.
  • Not checking join keys, duplicate rows or NULL values before trusting results.

FAQ

Will this SQL work in every database?

The idea is portable, but function names and date syntax may vary. Check your database dialect if a function is not recognized.

Should I use this in a report query?

Yes, if the pattern matches the business question and you have checked filters, joins and row counts.

Why does my result have too many rows?

The most common reasons are duplicate join keys, missing filters or grouping at the wrong level of detail.

💡 Useful resources

Here are some ideas for you

Optional resources that may help if you are learning SQL, building reports, writing queries or improving your data workflow.

  • 📘
    SQL books for beginners

    Practice query patterns with structured examples and exercises.

    See ideas
  • 🧱
    Database design books

    Understand tables, keys, relationships and why joins behave the way they do.

    See ideas
  • ⌨️
    Mechanical keyboards

    Useful if you write queries, code and documentation for long work sessions.

    See ideas
  • 🖥️
    External monitors

    View query editor, result grid and documentation side by side.

    See ideas
  • 📒
    Developer notebooks

    Sketch table relationships, query logic and report ideas before coding.

    See ideas
  • 💡
    Desk lamps

    Keep your workspace comfortable while studying or debugging queries.

    See ideas

Some links in this section may be affiliate links. Choose only what is useful for your own work.