How to Write Excel SUMIFS & COUNTIFS in SQL (Using CASE WHEN)
Need conditional totals across multiple columns in one SQL query? SUM(CASE WHEN ...) is the exact SQL twin of Excel SUMIFS.
By Victor Wachira · Founder, Sumwise · 5 min read
Conditional totals side-by-side
In Excel you write =SUMIFS(C:C, A:A, "North") next to =SUMIFS(C:C, A:A, "South"). In SQL, wrap a CASE WHEN inside SUM: SUM(CASE WHEN region = 'North' THEN revenue ELSE 0 END) AS north_rev.
COUNTIFS in one line
Replace THEN revenue with THEN 1 ELSE 0 END inside SUM() — or use COUNT(CASE WHEN status = 'paid' THEN 1 END) — to compute clean conversion rates in a single query.
Put this into practice in 4 minutes
Try real interactive Excel, Google Sheets, and SQL exercises — every core lesson is free to study.
Free 1-Page Excel, Google Sheets & SQL Interview Cheat Sheet
The 25 formulas, keyboard shortcuts, and SQL patterns tested most in analyst interviews — emailed instantly, zero spam.