typestar

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

  1. COUNT(*) counts rows; COUNT(col) skips NULLs.
  2. ROUND keeps a money average readable.
  3. With no GROUP BY, the whole table is the group.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 4 in Aggregation, step 1 of 22 in Joins & aggregation.

Next →