CASE Exercise in SQL
Master SQL CASE statements for advanced data categorization
This tutorial demonstrates how to use SQL CASE statements to create custom categories and regions from raw data, including advanced techniques for grouping and joining tables.
CASE Statement Applications
Regional Grouping
Create geographical categories like West Coast and East Coast from individual state data. Perfect for sales analysis and regional reporting.
User Segmentation
Divide users into Early, Middle, and Late adopter groups based on account creation dates for targeted analysis.
Cross-Table Analysis
Combine user categorization with order data using JOINs to analyze purchasing behavior by user segments.
Simple vs Searched CASE Statements
| Feature | Simple CASE | Searched CASE |
|---|---|---|
| Syntax | CASE column WHEN value | CASE WHEN condition |
| Use Case | Direct equality checks | Complex conditions (>, <, <=) |
| Example | state = 'CA' | year <= 2019 |
| Flexibility | Limited | High |
Building Regional Categories
Identify Source Column
Start with the ship_state column in the Orders table to create geographical groupings.
Use IN Operator
Group multiple states in one WHEN clause using IN instead of multiple OR conditions for cleaner code.
Apply Code Editor Tips
Select multiple values and use quote shortcuts in good editors like DBeaver to wrap selections in quotes automatically.
Add ELSE Clause
Include an ELSE statement to catch all remaining states that don't fit your defined categories.
Good code editors like DBeaver allow you to select text and hit quotes to automatically wrap selections. This feature isn't available in all editors like Microsoft SQL Server Management Studio.
Sample Regional Distribution
When using GROUP BY with CASE statements, you can only display the grouped column and aggregate functions. This forces you to think about what metrics matter most for your analysis.
User Segmentation by Date
Extract Date Component
Use DATE_PART function to extract just the year from the created_at timestamp for cleaner comparisons.
Define Time Periods
Create Early (2019 and earlier), Middle (2020), and Late (everything else) user categories.
Use Searched CASE
Since you're using <= conditions rather than simple equality, employ searched CASE syntax with conditions in WHEN clauses.
Include Reference Column
Add the created_at column temporarily for spot-checking your logic and ensuring categories are correct.
User Distribution by Signup Period
Pay attention to what you're counting. When grouping users by signup date, you're counting users (100 total). When you JOIN with orders, you're counting orders (500 total).
JOINing User Categories with Order Data
When joining tables with similar column names like created_at, SQL throws an ambiguous error. Always specify the table name (users.created_at) to resolve conflicts.
Orders by User Segment
SQL Query Development Strategy
Follow Execution Order
Write queries in the order SQL executes them: FROM, JOIN, WHERE, GROUP BY, SELECT to see transformations step by step.
View Joins First
Execute the FROM and JOIN clauses alone before adding GROUP BY to understand how tables combine.
Add Grouping Last
GROUP BY hides intermediate details, so add it after you've verified your joins work correctly.
Use Reference Columns
Include extra columns temporarily for spot-checking logic, then remove them in the final query.
If I just do things in the order of execution, I'll be able to see things as long as possible. That's not an obvious thing that everybody figures out.
This lesson is a preview from our SQL Course Online (includes software) and SQL Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.
Key Takeaways