Counting and totals
Aggregate functions turn many rows into one value:
| Function | Gives |
|---|---|
COUNT(*) |
how many rows |
SUM(column) |
the total |
AVG(column) |
the average |
MIN(column) |
the smallest |
MAX(column) |
the biggest |
SELECT COUNT(*) FROM customers; -- 8
SELECT SUM(quantity) FROM orders; -- items sold
SELECT MAX(price) FROM products WHERE category = 'Furniture';
Naming result columns with AS
A result column is named after its expression, like COUNT(*). AS gives it a readable name:
SELECT COUNT(*) AS customers, MIN(joined) AS first_joined FROM customers;
Rounding
AVG often gives long decimals. ROUND(value, 2) rounds to 2 decimal places.
Your task
Write one query that returns a single row with:
| Column name | Value |
|---|---|
product_count |
the number of products |
cheapest |
the lowest price |
priciest |
the highest price |
average_price |
the average price, rounded to 2 decimals |