Home › Learn › SQL › Aggregates

SUM, AVG, MIN, MAX

Lesson 12 of 26. Totals, averages, and extremes.

SUM(column) adds a column up, AVG averages it, MIN and MAX find the smallest and largest. Several can share one query: SELECT SUM(stock), AVG(price) FROM products; returns one row with both numbers. NULLs are ignored by all of them.

Mixing an aggregate with a plain column, SELECT name, MAX(price), is a classic mistake: SQLite returns the name from the max row, other databases refuse. The right tool for "which product is the most expensive" is ORDER BY price DESC LIMIT 1 or a subquery.

SELECT MIN(salary), MAX(salary), AVG(salary) FROM employees;

Your task

In one query on products, show the total stock, the average price, the lowest price, and the highest price, in that order.

Hint 1

SUM(stock), AVG(price), MIN(price), MAX(price)

Show the solution
SELECT SUM(stock), AVG(price), MIN(price), MAX(price) FROM products;

    Runs SQLite compiled to WebAssembly in this tab; the database is rebuilt before every run.