Aggregate functions in SQL
COUNT, SUM, AVG, MIN and MAX collapse many rows into one summary row.
SELECT
COUNT(*) AS orders,
SUM(total) AS revenue,
ROUND(AVG(total), 2) AS average_order,
MIN(placed_at) AS first_order,
MAX(placed_at) AS latest_order
FROM orders
WHERE status = 'paid';
How it works
COUNT(*)counts rows;COUNT(col)skips NULLs.ROUNDkeeps a money average readable.- With no
GROUP BY, the whole table is the group.
Keywords and builtins used here
ASAVGCOUNTFROMMAXMINSELECTSUMWHERE
The run, in numbers
- Lines
- 8
- Characters to type
- 185
- Tokens
- 47
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 35 seconds.
Step 1 of 4 in Aggregation, step 1 of 22 in Joins & aggregation.