Excel to SQL · Lesson 5 of 12

COUNTIF and SUMIF are WHERE plus a total

In Excel, you'd reach for
fx=COUNTIF(A:A, "Germany"), =SUMIFS(D:D, B:B, "Europe")

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_orderseurope_units
137416

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.

This page on one card

COUNTIF and SUMIF are WHERE plus a total: the Excel way (=COUNTIF(A:A) and the SQL query, side by side
COUNTIF and SUMIF are WHERE plus a total, on one card: the Excel way and the SQL. Download · Save to Pinterest
The practice data (7 tables)

See every column and sample rows