How to pivot a table in SQL with CASE WHEN and GROUP BY
GROUP BY gives you long lists: one row per month and status. A report usually wants a cross table instead, the way an Excel PivotTable shows it, with months down the side and statuses across the top. In SQL you build each of those columns as its own sum, and CASE WHEN decides which rows count toward it.
In short: every column of the cross table is a separate sum, such as SUM(CASE WHEN status = 'open' THEN 1 ELSE 0 END) AS open. GROUP BY decides the rows, the CASE sums decide the columns.
What you should know first: GROUP BY and CASE WHEN, and for the second and third exercises joining several tables.
Try it now
Customer service wants to see, for every customer, how many orders sit in each status: one row per customer, one column per status. If you get stuck, read the next section and then come back.
Task Customer service wants to see at a glance where each customer's orders stand. Show one row per customer with customer_id and four columns counting how many of that customer's orders are delivered, shipped, open and cancelled (in that order). Where a customer has no order with that status, show 0.
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
How a pivot works in SQL
Start with the long version. This counts orders by month and status:
SELECT substr(order_date, 1, 7) AS month, status, COUNT(*) AS orders
FROM orders
GROUP BY month, status;
That returns nine rows, two of them for February: 2026-02 delivered 2 and 2026-02 cancelled 1. For a report you want each month on a single line with the statuses side by side. So you group by month only and give every status a column of its own:
SELECT substr(order_date, 1, 7) AS month,
SUM(CASE WHEN status = 'delivered' THEN 1 ELSE 0 END) AS delivered,
SUM(CASE WHEN status = 'shipped' THEN 1 ELSE 0 END) AS shipped,
SUM(CASE WHEN status = 'open' THEN 1 ELSE 0 END) AS open,
SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled
FROM orders
GROUP BY month;
| month | delivered | shipped | open | cancelled |
|---|---|---|---|---|
| 2026-01 | 3 | 0 | 0 | 0 |
| 2026-02 | 2 | 0 | 0 | 1 |
| 2026-03 | 3 | 0 | 0 | 0 |
| 2026-04 | 2 | 0 | 0 | 0 |
| 2026-05 | 1 | 0 | 0 | 1 |
| 2026-06 | 0 | 2 | 3 | 0 |
Read one column like this: for every order in the month, CASE WHEN checks whether the status matches. If it does, you get 1, otherwise 0, and SUM adds up the ones. That's the count. If you want an amount rather than a count, put the amount after THEN, for example quantity * unit_price.
The ELSE 0 is there for a reason. Without ELSE, CASE returns NULL for rows that don't match. SUM skips NULLs, so the total is still right as long as at least one row matches. If none does, though, the result is NULL instead of 0 and the report shows an empty cell.
How this maps to an Excel PivotTable
If you've built PivotTables in Excel, every part is still there. You just write it out yourself:
| Excel PivotTable | SQL |
|---|---|
| Rows | column in GROUP BY (here the month) |
| Columns | one SUM(CASE WHEN … END) line per value |
| Values as Sum or Count | SUM with the amount, or with 1, after THEN |
| Filters | WHERE |
| Grouping items | CASE WHEN status IN ('shipped', 'open') |
The big difference is that Excel creates the columns for you from whatever values turn up in the data. In SQL you name every column. If a new status such as "returned" appears tomorrow, it won't show up in your query until you add a line for it. That cuts both ways: your report always has the same columns, even in a month with no cancellations.
One more: months as columns
Now flip it around. Categories become rows, months become columns, and instead of counting you add up revenue. This one needs three tables.
Task Purchasing wants revenue by category for the first half of 2026, laid out like a pivot table: one row per category and one column each for January to June (jan, feb, mar, apr, may, jun). Revenue is quantity times unit_price from the order items, and cancelled orders don't count. Months without revenue show 0.
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Putting two values in one column
In the last exercise one column covers two statuses. IN handles that in a single condition.
Task Management wants to know how the order value of each customer segment splits by processing stage. Show one row per segment with three columns: delivered (value of delivered orders), pending (value of orders that are shipped or open) and cancelled (value of cancelled orders). An order's value is the sum of quantity times unit_price over its items. Where there's nothing, show 0.
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Common mistakes
Grouping by the column that should become columns
GROUP BY customer_id, status gives you the long list again, one row per customer and status. The exercise then says your query returns too many rows. GROUP BY only gets what makes up one row of the cross table, not the value you're spreading across the columns.
COUNT with ELSE 0
COUNT(CASE WHEN status = 'open' THEN 1 ELSE 0 END) counts every order, because COUNT counts anything that isn't NULL, and 0 isn't NULL. The exercise reports that columns and row count match but not the values. Use SUM with ELSE 0, or COUNT with no ELSE.
Leaving out GROUP BY
Without GROUP BY, SUM rolls the whole table into a single row, and the exercise says your query returns only 1 row. The condition isn't the problem here; the grouping is missing.
Filtering cancellations too early
If one column is meant to show cancelled orders, there can't be a WHERE status <> 'cancelled' in front of it, or that column stays 0 everywhere. Use WHERE only for rows that belong in none of the columns.
Pivoting in MySQL, PostgreSQL, SQL Server and SQLite
The CASE WHEN approach runs the same way in all four. Each also has its own extras:
| Database | Built-in pivot | Shorter conditional sum |
|---|---|---|
| MySQL | none; the SELECT syntax has no PIVOT clause | SUM(IF(status = 'open', 1, 0)) |
| PostgreSQL | crosstab() from the tablefunc extension | COUNT(*) FILTER (WHERE status = 'open') |
| SQL Server | the PIVOT operator | SUM(IIF(status = 'open', 1, 0)); IIF is shorthand for CASE |
| SQLite (these exercises) | none | FILTER (WHERE …) since version 3.30.0 |
Here is the first exercise written with PIVOT in SQL Server:
SELECT customer_id, [delivered], [shipped], [open], [cancelled]
FROM (SELECT order_id, customer_id, status FROM orders) AS src
PIVOT (COUNT(order_id)
FOR status IN ([delivered], [shipped], [open], [cancelled])) AS pvt;
The columns are still spelled out, this time in the list after IN. Microsoft notes that NULL values in the value column are ignored when the aggregate is calculated. In PostgreSQL, crosstab turns a query with three columns (row name, category, value) into a cross table, and you declare the names and data types of the output columns yourself in the FROM clause. The exercises on this page check for CASE, because that version runs everywhere.
Sources: Microsoft Learn (Using PIVOT and UNPIVOT, IIF), PostgreSQL documentation (tablefunc, Aggregate Expressions), MySQL Reference Manual (SELECT Statement, Flow Control Functions), sqlite.org (SELECT, Aggregate Functions, release 3.30.0).
FAQ
Can SQL create the columns automatically from the data?
Not in a plain query. The columns of a result are fixed before any data is read. If you need columns that depend on the data, you build the query text in a program or a stored procedure first. For reports with a fixed set of columns, such as twelve months, CASE WHEN is simpler.
SUM or COUNT: which is better?
Either works. SUM(CASE WHEN … THEN 1 ELSE 0 END) and COUNT(CASE WHEN … THEN 1 END) give the same count. You need SUM for amounts anyway, so sticking to SUM keeps things consistent.
How do I add a total column?
Add one more aggregate without CASE, for example COUNT(*) AS total or SUM(quantity * unit_price) AS total. It covers every row in the group, whatever the status.
Can I go the other way, from columns to rows?
Yes, that's called unpivoting. SQL Server has an UNPIVOT operator for it. In any database you can do it with UNION ALL: one query per column that returns the column name as text plus its value.