Feature Flag Adoption
An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- easy
- Seniority
- L3
- Asked in
- Airbnb
Problem
Count how many flags are enabled and how many are disabled for each feature, so we can see where every feature stands before the next major rollout. Each feature is gated by several flags in feat_flags, one row per flag, all carrying the feature's flag_name.
How widely adopted are the flags?
Tables
feat_flags
Columns: flag_id INTEGER, flag_name TEXT, enabled INTEGER, rollout REAL, owner TEXT, created TEXT, updated TEXT
| flag_id | flag_name | enabled | rollout | owner | created | updated |
|---|---|---|---|---|---|---|
| 131 | new_checkout | 0 | NULL | engineering | 2025-02-02 | 2026-02-02 |
| 162 | beta_search | 0 | NULL | growth | 2025-03-03 | 2026-03-03 |
| 193 | ai_recs | 1 | 39 | platform | 2025-04-04 | 2026-04-04 |
| 224 | live_chat | 0 | NULL | mobile | 2025-05-05 | 2026-05-05 |
| 255 | video_upload | 0 | NULL | data | 2025-06-06 | 2026-06-06 |
| 286 | two_factor | 1 | 78 | product | 2025-07-07 | 2026-07-07 |
| 317 | bulk_export | 0 | NULL | engineering | 2025-08-08 | 2026-08-08 |
| 348 | new_dashboard | 0 | NULL | growth | 2025-09-09 | 2026-09-09 |
| 379 | mobile_v2 | 1 | 17 | platform | 2025-10-10 | 2026-01-10 |
| 410 | dark_mode | 0 | NULL | mobile | 2025-11-11 | 2026-02-11 |
Expected output
| flag_name | enabled | flag_count |
|---|---|---|
| ai_recs | 0 | 12 |
| ai_recs | 1 | 8 |
| beta_search | 0 | 14 |
| beta_search | 1 | 6 |
| bulk_export | 0 | 14 |
Worked solution and explanation
What this is really asking
This is a two-key count dressed up as a rollout inventory. Everyone reads 'enabled versus disabled' and splits on enabled. That returns two rows, 134 off and 66 on, and tells you nothing about which feature is dark. The detail that separates candidates is in the data: flag_name is not unique, and each of the ten features is gated by 20 flags. The grain the inventory needs is feature and state together. Miss it and either the features disappear into two totals, or the disabled side disappears into a per-feature on-count.
Build it
Step 1: Find the grain in the data
flag_id is unique but flag_name repeats on 20 rows per name. A feature's standing is the mix of its flags, so the output needs one row per (flag_name, enabled) pair.
Step 2: Split on both keys
GROUP BY flag_name, enabled creates a group for each pair. Every feature on the seed has flags in both states, so ten features give 20 rows.
Step 3: Count rows, not values
COUNT(*) counts the flags in each group. enabled is 0 or 1 with no NULLs, so there is no third group to guard against. Summing enabled instead would turn every off group into 0.
SELECT flag_name, enabled, COUNT(*) AS flag_count
FROM feat_flags
GROUP BY flag_name, enabledSELECT enabled, COUNT(*) AS flag_count
FROM feat_flags
GROUP BY enabledReturns 134 off and 66 on, and no feature is visible.
Follow-ups
Common follow-up questions
- What if the result should be one row per feature with separate on and off columns? (Use conditional sums such as `SUM(CASE WHEN enabled = 1 THEN 1 ELSE 0 END)`, grouped by `flag_name` only.)
- How do you show the share of each feature's flags that are on? (`AVG(enabled)` per `flag_name`, since the flag is 0/1.)
- How would you limit the inventory to flags owned by one team? (Add `WHERE owner = 'platform'` before grouping.)