SQL's if/elif/else
CASE WHEN is SQLite's only conditional expression in SELECT lists. Two shapes:
- Simple CASE:
CASE col WHEN v1 THEN r1 WHEN v2 THEN r2 ELSE r3 END— equality on a single column. - Searched CASE:
CASE WHEN cond1 THEN r1 WHEN cond2 THEN r2 ELSE r3 END— arbitrary boolean conditions. More flexible, what you'll use 90% of the time.
Tip: CASE inside aggregates is incredibly powerful —
sum(CASE WHEN status='paid' THEN total END) gives you a conditional sum without subqueries. This pattern shows up in almost every reporting query you'll ever write.