A retention squad triages accounts by how long they have been inactive and whether auto-renew is switched on.
From subscriptions, return account_code, days_inactive, auto_renew and a risk_band computed as:
Criticalwhendays_inactiveis 60 or more andauto_renewis falseHighwhendays_inactiveis 60 or more (auto-renew on)Watchwhendays_inactiveis between 30 and 59Healthyotherwise
Sample data
Table: subscriptions
14 row(s) · showing first 8
| sub_idINT | account_codeTEXT | planTEXT | days_inactiveINT | auto_renewBOOLEAN |
|---|---|---|---|---|
| 1 | AC-501 | Starter | 4 | true |
| 2 | AC-502 | Growth | 34 | true |
| 3 | AC-503 | Growth | 61 | false |
| 4 | AC-504 | Scale | 88 | true |
| 5 | AC-505 | Starter | 29 | false |
| 6 | AC-506 | Scale | 59 | true |
| 7 | AC-507 | Growth | 60 | true |
| 8 | AC-508 | Starter | 120 | false |
subscriptions
- sub_idINT
- account_codeTEXT
- planTEXT
- days_inactiveINT
- auto_renewBOOLEAN
- Step 1: List the bands from most specific to least specific — `CASE` short-circuits on the first true branch, so ordering *is* the logic.
- Step 2: The `Critical` branch needs both conditions: `days_inactive >= 60 AND NOT auto_renew`.
- Step 3: The `High` branch can then simply test `days_inactive >= 60`, because any row reaching it already has auto-renew on.
- Step 4: `Watch` becomes `days_inactive >= 30`; the upper bound of 59 is implied by the earlier branch.
- Step 5: `ELSE 'Healthy'` catches everything under 30 days. Alias the expression as `risk_band`.
SELECT account_code,
days_inactive,
auto_renew,
CASE
WHEN days_inactive >= 60 AND NOT auto_renew THEN 'Critical'
WHEN days_inactive >= 60 THEN 'High'
WHEN days_inactive >= 30 THEN 'Watch'
ELSE 'Healthy'
END AS risk_band
FROM subscriptions;Learn the concept first
Stuck on CASE Statements? These free SQL lessons teach it step by step.
More CASE Statements questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program