Excel to SQL · Lesson 5 of 12
COUNTIF and SUMIF are WHERE plus a total
COUNTIF is two steps squashed together: filter, then count. SQL keeps the steps visible: WHERE filters, COUNT counts.
Try itEdit the query, then Run
| europe_orders | europe_units |
|---|---|
| 137 | 416 |
1 row
Need several COUNTIFs side by side, like a little dashboard row? Put the condition inside the total with CASE:
SELECT
SUM(CASE WHEN region = 'Europe' THEN quantity ELSE 0 END) AS europe,
SUM(CASE WHEN region = 'Asia Pacific' THEN quantity ELSE 0 END) AS apac
FROM orders;Your turn
Count the support tickets with priority urgent. Call it urgent_tickets.
FROM tickets WHERE priority = 'urgent', then COUNT(*).
This page on one card
