Order Status Pivot
Return a single row with the number of orders in each status: delivered, shipped, paid, pending and cancelled (five columns, in this order).
Hint 1
Inside an aggregate, a CASE can give 1 for rows you want to count and 0 for the others.
Hint 2
SUM(CASE WHEN status = 'paid' THEN 1 ELSE 0 END) counts the paid orders.
Reference solution
SELECT 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 = 'paid' THEN 1 ELSE 0 END) AS paid,
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) AS pending,
SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled
FROM orders;
GROUP BY status gives the numbers as rows. A report often needs them as columns (a pivot). Conditional aggregation does that: each column is its own SUM(CASE ...) over the same rows. It also lets you put several different conditions into one pass over the table, instead of running five queries.
Another way. In SQLite a comparison is 1 or 0, so you can add the comparisons up directly. This shortcut is not available in every database; the CASE form works everywhere.
SELECT SUM(status = 'delivered') AS delivered,
SUM(status = 'shipped') AS shipped,
SUM(status = 'paid') AS paid,
SUM(status = 'pending') AS pending,
SUM(status = 'cancelled') AS cancelled
FROM orders;