SQL · Level 1 Aggregation

SQL COUNT, SUM and AVG

Summarize the orders in one row: count them, count those with a coupon and average their totals, knowing exactly which rows NULL leaves out.

  • COUNT(*), SUM(), AVG()
  • COUNT(column) skips NULL
  • ROUND() on an average

Aggregates turn many rows into one: COUNT(*), SUM(), AVG(), MIN(), MAX(). Trap: COUNT(column) skips NULLs, so COUNT(coupon) counts only the orders that used one, while COUNT(*) counts every row.

SELECT COUNT(*)      AS orders,       -- every row
       COUNT(coupon) AS with_coupon,  -- NULLs skipped
       MAX(total)    AS biggest
FROM orders;

Return one row: the number of orders, the number of orders that used a coupon, and the average total rounded to 2 decimals. In the database below, 2 of the 4 orders used a coupon.

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

orders 4 rows
idINTEGERcouponTEXTtotalREAL
1SPRING40.0
2NULL25.5
3VIP100.0
4NULL12.1
solution.sql
Test Suites Pending
Output