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 END5 practical examples
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.
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.
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.
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.
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.
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 beginnersSee ideas
Practice query patterns with structured examples and exercises.
- Database design booksSee ideas
Understand tables, keys, relationships and why joins behave the way they do.
- Mechanical keyboardsSee ideas
Useful if you write queries, code and documentation for long work sessions.
- External monitorsSee ideas
View query editor, result grid and documentation side by side.
- Developer notebooksSee ideas
Sketch table relationships, query logic and report ideas before coding.
- Desk lampsSee ideas
Keep your workspace comfortable while studying or debugging queries.
Some links in this section may be affiliate links. Choose only what is useful for your own work.