LQ
SQL Reference
Practice →
SQL ReferenceAggregationAggregate Functions

Aggregate Functions

Compute a single value from many rows.

Questions this answers
  • How do I count the total number of rows?
  • How do I find the average, sum, min, or max?
  • How do I count only distinct values?
  • How do aggregates handle NULLs?

How it works

Aggregate functions collapse multiple rows into one value. COUNT, SUM, AVG, MIN, MAX — each takes a column and returns a single number. On their own (without GROUP BY) they operate on the entire table.

The five core aggregates

SELECT
  COUNT(*)             AS total_rows,
  COUNT(label)         AS with_label,
  AVG(monthly_listeners) AS avg_listeners,
  SUM(monthly_listeners) AS total_listeners,
  MIN(monthly_listeners) AS min_listeners,
  MAX(monthly_listeners) AS max_listeners
FROM artists;

Count distinct values

SELECT COUNT(DISTINCT genre) AS unique_genres
FROM artists;
💡 COUNT(DISTINCT col) counts unique non-NULL values.

Ready to practise Aggregate Functions?

Solve real exercises with AI feedback — free to start.

Try it free →