Creating Flags
& Buckets in SQL
Learn how to categorize data by creating binary flags and range-based buckets from numeric and date fields using CASE expressions.
What Is a Flag?
A flag is a new column that holds a binary value: it is either 1 (true) or 0 (false). Flags answer simple yes/no questions about each row.
What Is a Bucket?
A bucket (also called a bin) groups rows into named categories based on ranges of a numeric or date value. Instead of two outcomes, buckets can have many categories.
Why Do Flags & Buckets Matter?
Raw numbers are hard to summarize. Flags and buckets let you transform continuous data into categories, which makes grouping, filtering, and reporting much easier.
The Core Tool: CASE Expression
Both flags and buckets are built using the SQL CASE expression. It evaluates conditions in order and returns the first matching result.
The Flag Pattern
A flag uses CASE with a single condition. It returns 1 when the condition is true and 0 otherwise.
The Bucket Pattern
A bucket uses CASE with multiple conditions, each defining a range. The conditions must be ordered carefully so ranges don't overlap.
Flags vs. Buckets
| Flag | Bucket | |
|---|---|---|
| Possible values | 2 (\(0\) or \(1\)) | Many (named categories) |
| Number of WHEN clauses | 1 | 2 or more |
| Output data type | Integer (0/1) | Text (string labels) |
| Best used for | Counting, filtering | Grouping, reporting |
| Example question | "Is this order high-value?" | "What tier is this order?" |
Step-by-Step Approach
Example: Date-Based Flag
Flag all users who signed up in the last \(30\) days as "new users."
Example: Date-Based Bucket
Categorize users by account age into tenure buckets.
Using Flags with Aggregation
Since flags are \(0\)s and \(1\)s, you can use SUM and AVG on them to get counts and percentages.
Using Buckets with GROUP BY
Buckets become truly powerful when you GROUP BY the bucket column to get aggregate stats per category.
Advanced: Multiple Flags
You can create several flags at once in the same SELECT. Each CASE expression becomes its own column.
Common Mistakes
ELSE, unmatched rows get NULL instead of \(0\). Always include ELSE 0 for flags and ELSE 'Other' for buckets.\(\ge 50\) AND \(\le 100\) on one WHEN and \(\ge 100\) on the next creates ambiguity at the boundary. Use strictly ordered \(\lt\) comparisons.Summary Reference
| Pattern | Key Detail | |
|---|---|---|
| Flag | CASE WHEN ... THEN 1 ELSE 0 END |
Binary: \(0\) or \(1\) |
| Bucket | CASE WHEN ... THEN 'label' ... END |
Multiple string categories |
| Date flag | WHEN col >= CURRENT_DATE - N |
Use date arithmetic |
| Date bucket | Check most recent first | Order: recent to oldest |
| SUM(flag) | Counts true rows | Works because \(1+1+0 = 2\) |
| AVG(flag) | Percentage of true | Use 1.0 to avoid integer division |
| GROUP BY bucket | Distribution report | One row per category |
Question 1 of 5
What does a flag column typically contain?
Question 2 of 5
Given this SQL fragment, what label does an order with total = 150 receive?
Question 3 of 5
What happens if you write a CASE expression without an ELSE clause and no WHEN condition matches?
Question 4 of 5
If you wrap a flag in SUM(), what does the result represent?
SUM adds up all the \(1\)s, giving you the count of rows meeting the condition. For example, SUM returns \(3\) if exactly \(3\) rows are flagged as \(1\).Question 5 of 5
You want to bucket users by signup date, checking the most recent dates first. Why is this order important?
signup_date >= CURRENT_DATE - 365 before signup_date >= CURRENT_DATE - 30, the broader condition catches recent signups too, and they never reach the more specific bucket.