COUNT_IF conditional counting
Concepts covered: Conditional Counting
COUNT_IF(condition) evaluates a true-or-false expression on every row of the group and counts the rows where it came out true. COUNT_IF(skipped) counts skipped plays; COUNT_IF(ms_played >= 30000) counts plays that ran at least 30 seconds. Rows where the condition is false are not counted, and neither are rows where it's NULL, so only a definite true adds 1.
SELECT
listener_id,
COUNT(*) AS plays_total,
COUNT_IF(skipped) AS skips,
COUNT_IF(ms_played >= 30000) AS streams
FROM plays
GROUP BY listener_id
ORDER BY listener_id| listener_id | track | genre | played_at | ms_played | skipped |
|---|---|---|---|---|---|
| ana | Aurora | pop | 08:10 | 200000 | 0 |
| ana | Basalt | rock | 08:14 | 15000 | 1 |
| ana | Cinder | rock | 08:20 | 240000 | 0 |
| ana | Aurora | pop | 09:02 | 198000 | 0 |
| ben | Delta | jazz | 07:45 | 310000 | 0 |
| ben | Ember | pop | 07:51 | 9000 | 1 |
| ben | Fjord | jazz | 08:30 | 12000 | 1 |
| cy | Basalt | rock | 10:00 | 230000 | 0 |
| listener_id | plays_total | skips | streams |
|---|---|---|---|
| ana | 4 | 1 | 3 |
| ben | 3 | 2 | 1 |
| cy | 1 | 0 | 1 |
One pass over the table produces 3 different counts per listener, each with its own condition, and every row is read once. Counting with WHERE would take a separate query per condition, because a WHERE clause trims the rows that all aggregates see; COUNT_IF keeps all 3 counts inside one GROUP BY. The last section shows a second one-pass route. ben has 3 plays, and only 1 of them counts as a stream: Ember and Fjord, his 2 short plays, both ran under 30000 ms, and he skipped both, which gives his skip count of 2.
COUNT_IF versus WHERE
WHERE can count matching rows too, but it does so by removing the others before grouping. The difference shows up at zero. cy skipped nothing, so WHERE skipped removes every one of cy's rows, and a group with no rows produces no output row. COUNT_IF keeps cy's play in the group and reports 0.
| listener_id | track | genre | played_at | ms_played | skipped |
|---|---|---|---|---|---|
| ana | Aurora | pop | 08:10 | 200000 | 0 |
| ana | Basalt | rock | 08:14 | 15000 | 1 |
| ana | Cinder | rock | 08:20 | 240000 | 0 |
| ana | Aurora | pop | 09:02 | 198000 | 0 |
| ben | Delta | jazz | 07:45 | 310000 | 0 |
| ben | Ember | pop | 07:51 | 9000 | 1 |
| ben | Fjord | jazz | 08:30 | 12000 | 1 |
| cy | Basalt | rock | 10:00 | 230000 | 0 |
WHERE
Rows are removed before grouping, so cy has no rows left and disappears from the result.
SELECT
listener_id,
COUNT(*) AS skips
FROM plays
WHERE skipped
GROUP BY listener_id
ORDER BY listener_id| listener_id | skips |
|---|---|
| ana | 1 |
| ben | 2 |
COUNT_IF
Every listener keeps a group, so cy appears with 0 skips.
SELECT
listener_id,
COUNT_IF(skipped) AS skips
FROM plays
GROUP BY listener_id
ORDER BY listener_id| listener_id | skips |
|---|---|
| ana | 1 |
| ben | 2 |
| cy | 0 |
Pick by the question. 'How many skips did each skipping listener make?' wants WHERE, and the smaller input makes it cheaper. 'What is every listener's skip count?' wants COUNT_IF, because a dashboard that silently omits zero-skip listeners reads as though they do not exist. WHERE is the better fit when a single filter should apply to every aggregate in the query at once.
Rates need a cast
A skip rate divides skips by plays. COUNT_IF and COUNT(*) both return BIGINT, and dividing a BIGINT by a BIGINT performs integer division, which drops the fraction. ben's 2 skips over 3 plays come out as 0. CAST one side to DOUBLE before dividing and the rate keeps its fraction.
What does this query return in skip_rate for ana, who has 1 skip in 4 plays?
SELECT
listener_id,
COUNT_IF(skipped) / COUNT(*) AS skip_rate
FROM plays
GROUP BY listener_id- 0.25, because dividing 2 counts yields a fraction automatically
- 0, because BIGINT divided by BIGINT drops the fraction
- 1, because integer division rounds 0.25 up to a whole number
- An error, because COUNT_IF cannot sit inside arithmetic
Cast the numerator and the same division keeps its fraction: the query below returns 0.25 for ana, roughly 0.67 for ben, and 0.0 for cy, whose only play was never skipped.
SELECT
listener_id,
CAST(COUNT_IF(skipped) AS DOUBLE) / COUNT(*) AS skip_rate
FROM plays
GROUP BY listener_id
ORDER BY listener_id| listener_id | track | genre | played_at | ms_played | skipped |
|---|---|---|---|---|---|
| ana | Aurora | pop | 08:10 | 200000 | 0 |
| ana | Basalt | rock | 08:14 | 15000 | 1 |
| ana | Cinder | rock | 08:20 | 240000 | 0 |
| ana | Aurora | pop | 09:02 | 198000 | 0 |
| ben | Delta | jazz | 07:45 | 310000 | 0 |
| ben | Ember | pop | 07:51 | 9000 | 1 |
| ben | Fjord | jazz | 08:30 | 12000 | 1 |
| cy | Basalt | rock | 10:00 | 230000 | 0 |
| listener_id | skip_rate |
|---|---|
| ana | 0.25 |
| ben | 0.6666666666666666 |
| cy | 0 |
Conditions inside HAVING
COUNT_IF is an aggregate, so it can sit in HAVING the way COUNT(*) does. HAVING COUNT_IF(skipped) >= 2 keeps listeners with at least 2 skips; on the sample that is ben alone. The condition inside COUNT_IF looks at single rows, and HAVING then compares the finished count, so a single expression filters at both levels.
About this lesson
This section is part of the Aggregating: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.