SQL · Level 1 Project

Project: SQL Order Status Report

Build March's order report in one query: count the orders, count the shipped ones and total their revenue with conditional aggregation.

  • SUM(CASE WHEN ... THEN 1 ELSE 0 END)
  • Date range plus aggregates
  • One row, several numbers

To count several things in one pass, put CASE inside an aggregate: SUM(CASE WHEN ... THEN 1 ELSE 0 END) counts the matching rows. Trap: COUNT(CASE ...) counts the 0s too, because COUNT counts every value that isn't NULL.

SELECT COUNT(*) AS orders,
       SUM(CASE WHEN coupon IS NOT NULL THEN 1 ELSE 0 END) AS with_coupon,
       SUM(CASE WHEN coupon IS NULL THEN total ELSE 0 END) AS full_price_revenue
FROM orders;

For orders placed in March 2024, return one row: the number of orders, how many have shipped (shipped_at is set), and the revenue of the shipped ones rounded to 2 decimals. In the database below, March has 3 orders and 2 of them have shipped.

Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.

orders 5 rows
idINTEGERcreated_atTEXTtotalREALshipped_atTEXT
12024-02-28 10:0050.02024-03-01
22024-03-02 09:0020.52024-03-03
32024-03-10 14:0035.25NULL
42024-03-31 21:0044.252024-04-01
52024-04-01 08:0012.0NULL
solution.sql
Test Suites Pending
Output