HardSQL challenge #203CASEPIVOTAGGREGATION

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.

Solve This SQL Challenge

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

First 3 rows of the orders table
iduser_idtotaldiscountitem_countstatusorder_dateshipped_dateshipping_cityshipping_country
143321.680.259cancelled2024-04-24NULLDelhiIndia
2810.520.191delivered2024-03-152024-03-20ColumbusUSA
3136181.190.085delivered2023-02-152023-02-18ChicagoUSA

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
total_orderspendingprocessingshippeddeliveredcancelledrefunded
200232821822719

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:

  1. Read from the orders table.
  2. Aggregate over all the rows that are left: compute COUNT(*), a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows where status equals 'pending', a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows where status equals 'processing', a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows where status equals 'shipped', a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows where status equals 'delivered', a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows where status equals 'cancelled' and a conditional count (SUM(CASE WHEN … THEN 1 ELSE 0 END)) of the rows where status equals '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.

Solve This SQL Challenge

Keep learning