Pivot Order Status Counts into Columns with CASE WHEN
Return one row with the total number of orders and a separate count column for each of the six order statuses.
The challenge runs on the SQLabHub practice database (SQLite) and uses the orders table. You can solve it in the browser, with nothing to install.
Why this question comes up
An operations dashboard shows order counts by status as a single row of tiles: pending, processing, shipped and so on. The data has one row per order, so the query has to turn status values into columns.
The task
Count orders by status with one column per status (pending, processing, shipped, delivered, cancelled, refunded) plus a total, all in a single row.
A correct answer returns 1 row with 7 columns: total_orders, pending, processing, shipped, delivered, cancelled, refunded.
Tables used in this challenge
orders 200 rows
id INTEGER (PK) · user_id INTEGER · total REAL · discount REAL · item_count INTEGER · status TEXT · order_date TEXT · shipped_date TEXT · shipping_city TEXT · shipping_country TEXT
| id | user_id | total | discount | item_count | status | order_date | shipped_date | shipping_city | shipping_country |
|---|---|---|---|---|---|---|---|---|---|
The first rows of each table are shown. See how all the tables relate on the database schema page.
Expected output
| total_orders | pending | processing | shipped | delivered | cancelled | refunded |
|---|---|---|---|---|---|---|
The complete result: 1 row. This is the output of the model solution on the practice database.
What you practise
- Summarising many rows into one value with COUNT, SUM, AVG, MIN or MAX
- Turning conditions into values with CASE WHEN
Hint and approach
Try the challenge first. Open these only when you need them.
Show the hint
One SUM(CASE WHEN status = ... THEN 1 ELSE 0 END) per column
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Read from the
orderstable. - Aggregate over all the rows that are left: compute
COUNT(*), a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows wherestatusequals 'pending', a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows wherestatusequals 'processing', a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows wherestatusequals 'shipped', a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows wherestatusequals 'delivered', a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows wherestatusequals 'cancelled' and a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows wherestatusequals 'refunded'. With no GROUP BY, the result is a single row.
Show the model solution
SELECT COUNT(*) AS total_orders,
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) AS pending,
SUM(CASE WHEN status = 'processing' THEN 1 ELSE 0 END) AS processing,
SUM(CASE WHEN status = 'shipped' THEN 1 ELSE 0 END) AS shipped,
SUM(CASE WHEN status = 'delivered' THEN 1 ELSE 0 END) AS delivered,
SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled,
SUM(CASE WHEN status = 'refunded' THEN 1 ELSE 0 END) AS refunded
FROM orders;This query produced the expected output above. Yours does not have to match it word for word: any query that returns the same rows is correct.
What to watch for
SQLite has no PIVOT keyword. Each output column is a SUM over a CASE that yields 1 for the wanted status and 0 otherwise. The six status columns add up to total_orders, which is a quick check that no status was missed or misspelled.
- CASE WHEN. CASE stops at the first WHEN that is true, so overlapping conditions must be ordered from most to least specific. Without an ELSE, unmatched rows get NULL.
- Aggregate functions. Aggregates skip NULLs. AVG divides by the number of non-NULL values and COUNT(column) can be smaller than COUNT(*).
Another way to solve it
In SQLite and MySQL a comparison evaluates to 1 or 0, so SUM(status = 'pending') counts the matches. It is shorter but not portable: PostgreSQL needs a cast or FILTER, and SQL Server needs the CASE form.
Show the alternative query
SELECT COUNT(*) AS total_orders,
SUM(status = 'pending') AS pending,
SUM(status = 'processing') AS processing,
SUM(status = 'shipped') AS shipped,
SUM(status = 'delivered') AS delivered,
SUM(status = 'cancelled') AS cancelled,
SUM(status = 'refunded') AS refunded
FROM orders;Checked when this page was built: it returns the same rows as the model solution on the practice database.
Related SQL challenges
- Count Employees per Salary Band in Each DepartmentSimilar levelHard
- Calculate Payment Success Rate per Method with CASE WHENEasierMedium
Keep learning
- Lesson: CASE WHEN
- Lesson: Aggregate Functions
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database