Left On

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4
Asked in
Salesforce

Problem

Flags created more than 730 days before May 1, 2026 are what the platform team's left-on audit is chasing, and a flag with no updated timestamp still counts as on regardless of enabled. For each one, show its name, its owner, whether it's still enabled as 'Yes' or 'No', and its whole years since creation, taken as elapsed days divided by 365 and rounded down. The audit reads alphabetically by flag name and then owner, with the most whole years first where those match and switched-off flags ahead of running ones.

Some switches never got flipped back.

Tables

feat_flags

Columns: flag_id INTEGER, flag_name TEXT, enabled INTEGER, rollout REAL, owner TEXT, created TEXT, updated TEXT

feat_flags: 10 of 200 rows
flag_idflag_nameenabledrolloutownercreatedupdated
131new_checkout0NULLengineering2025-02-022026-02-02
162beta_search0NULLgrowth2025-03-032026-03-03
193ai_recs139platform2025-04-042026-04-04
224live_chat0NULLmobile2025-05-052026-05-05
255video_upload0NULLdata2025-06-062026-06-06
286two_factor178product2025-07-072026-07-07
317bulk_export0NULLengineering2025-08-082026-08-08
348new_dashboard0NULLgrowth2025-09-092026-09-09
379mobile_v2117platform2025-10-102026-01-10
410dark_mode0NULLmobile2025-11-112026-02-11

Expected output

First 5 of 48 rows
flag_nameownerstill_enabledyears_since_creation
ai_recsengineeringNo2
ai_recsengineeringNo2
ai_recsplatformYes2
ai_recsplatformYes2
beta_searchgrowthNo3

Worked solution and explanation

What this problem is really testing

This is a date-arithmetic problem wearing a hygiene costume. The skill being probed: can you measure elapsed time with JULIANDAY, filter on a strict day threshold without an off-by-one, and get the two-part still-enabled rule right? The trap is that a flag is on when enabled = 1 OR updated IS NULL, not just when enabled = 1. Miss the OR and every never-touched flag reads 'No', quietly hiding exactly the stale flags the audit exists to catch.


Breaking it down

Step 1: Filter to the stale flags

Keep only flags older than the threshold with JULIANDAY('2026-05-01') - JULIANDAY(created) > 730. The comparison is strict, so a flag exactly 730 days old is excluded. Subtracting JULIANDAY values rather than comparing the raw text dates is what makes the day count real.

Step 2: Derive still_enabled with the null rule

Build still_enabled with CASE WHEN enabled = 1 OR updated IS NULL THEN 'Yes' ELSE 'No' END. The OR is the whole point: a flag nobody ever touched has no updated timestamp, so it counts as on even when enabled reads 0.

Step 3: Convert days to whole years

Turn elapsed days into whole years with CAST((JULIANDAY('2026-05-01') - JULIANDAY(created)) / 365 AS INTEGER). The CAST truncates toward zero, giving completed years rather than a rounded fraction.

Step 4: Lay out the register

Sort by flag_name, then owner, then years_since_creation descending, then still_enabled, so the register reads the way the audit lays it out.


The solution

Date arithmetic with conditional formatting
SELECT flag_name, owner,
       CASE WHEN enabled = 1 OR updated IS NULL THEN 'Yes' ELSE 'No' END AS still_enabled,
       CAST((JULIANDAY('2026-05-01') - JULIANDAY(created)) / 365 AS INTEGER) AS years_since_creation
FROM feat_flags
WHERE JULIANDAY('2026-05-01') - JULIANDAY(created) > 730
ORDER BY flag_name, owner, years_since_creation DESC, still_enabled

Common follow-up questions