Tsallaka zuwa abun ciki
Justice Tech School

CASE Pivot

SUM with CASE turns categories into columns.

Example
SELECT country, SUM(CASE WHEN score >= 80 THEN 1 ELSE 0 END) AS high, SUM(CASE WHEN score < 80 THEN 1 ELSE 0 END) AS low
FROM students WHERE score IS NOT NULL GROUP BY country;

Result

country      | high | low
Ghana        | 0    | 1
Kenya        | 0    | 1
Nigeria      | 3    | 1
South Africa | 0    | 1

Exercise

Count matches.

SUM(CASE WHEN a THEN  ELSE 0 END)

Quiz Test

What does this pattern make?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.