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
- COUNT counts the rows: 4.
- SUM adds them: 100.
- AVG divides the sum by the count: 25.
- MIN and MAX find 10 and 40.
- 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
- Open the interactive lesson, with a mini quiz.
- Take the Databases quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Databases quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Aggregate Functions?
Where do people use Aggregate Functions?
What is the most common mistake with Aggregate Functions?
What should I learn before this topic?
How can I practice it?
Is Aggregate Functions hard to learn?
How long does it take to learn Aggregate Functions?
Which topics are related to Aggregate Functions?
What should I study after Aggregate Functions?
Related topics
- INSERT, UPDATE, DELETE (Databases)
- GROUP BY and HAVING (Databases)
- ORDER BY and LIMIT (Databases)
- Joins (Databases)
- Functions and Relations (Theory & Discrete Math)
- Functions and Graphs (Math for Computing)
- Functions (Programming Fundamentals)