Lesson 4 of 12 · 7 min

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
query.sql
-- One row summarising the products table

SQLite runs right here in your browser. The first run downloads about 0.7 MB; after that it is instant.

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…