LearnViz CompSci
Home / Learn / Databases / Aggregate Functions

Beginner Topic 12 of 51 Databases

Aggregate Functions

COUNT, SUM, AVG and similar summarizers.

How it works

Aggregate functions compute a single value from a set of rows: COUNT, SUM, AVG, MIN and MAX, along with others such as STRING_AGG and statistical functions. They collapse many rows into one per group, and most ignore NULL values, except COUNT(*), which counts rows. With no GROUP BY, they summarize the whole result. DISTINCT inside an aggregate, as in COUNT(DISTINCT user_id), counts unique values.

Step-by-step walkthrough

  1. COUNT counts the rows: 4.
  2. SUM adds them: 100.
  3. AVG divides the sum by the count: 25.
  4. MIN and MAX find 10 and 40.
  5. Aggregates collapse many rows into one value and ignore NULLs, except COUNT star.

Where it is used

Total revenue, average order value, highest score, number of active users and similar reports use aggregates.

Common pitfall

AVG ignores NULLs, so averages may not mean what you think. SUM on an empty set returns NULL, not zero, so use COALESCE. Integer division in averages truncates in some databases. Selecting a non-aggregated column not in GROUP BY is invalid or gives arbitrary values. Be careful with joins that duplicate rows and inflate sums.

Where it fits in the course

Aggregate Functions is topic 12 of 51 in Databases, rated Beginner. It follows INSERT, UPDATE, DELETE. The next topic is GROUP BY and HAVING.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Databases quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Aggregate Functions?
COUNT, SUM, AVG and similar summarizers. It belongs to Databases and is a beginner-level topic.
Where do people use Aggregate Functions?
Total revenue, average order value, highest score, number of active users and similar reports use aggregates.
What is the most common mistake with Aggregate Functions?
AVG ignores NULLs, so averages may not mean what you think.
What should I learn before this topic?
Start with INSERT, UPDATE, DELETE, the topic just before it in Databases.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Databases quiz.
Is Aggregate Functions hard to learn?
It is a Beginner topic, so it works as an entry point. If the terms are new, read the explanation twice and try the visualizer before the quiz.
How long does it take to learn Aggregate Functions?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Aggregate Functions?
INSERT, UPDATE, DELETE, GROUP BY and HAVING, ORDER BY and LIMIT are closely connected. The full list is in the related topics section of this page.
What should I study after Aggregate Functions?
Continue with GROUP BY and HAVING, the next topic in Databases.

Related topics