MODULE 3 OF 8: AGGREGATE QUERIES • LESSON 1 OF 4~18 min read

Aggregate Functions: COUNT, SUM, AVG, MIN and MAX

Aggregate functions turn many rows into a useful summary. They answer practical questions such as how many employees are recorded, how much revenue was collected, and what the average order value is. This lesson explains what each common aggregate calculates, how NULL affects the result, and when DISTINCT changes the meaning.

•

Aggregate functions turn many rows into a useful summary. They answer practical questions such as how many employees are recorded, how much revenue was collected, and what the average order value is. This lesson explains what each common aggregate calculates, how NULL affects the result, and when DISTINCT changes the meaning.

Key Takeaways at a Glance:

  • •Aggregate functions (COUNT, SUM, AVG, MIN, MAX) summarize multiple input rows into a single scalar value per group.
  • •COUNT(*) counts every physical row; COUNT(column) counts only rows where the specified column is NOT NULL.
  • •SUM and AVG ignore NULL values entirely; if all rows in a group are NULL, SUM/AVG return NULL.
  • •COUNT(DISTINCT column) computes the cardinality of unique non-NULL values.

In Simple Words (The Mental Model)

Think of aggregate functions as a blender. You drop 100 fruit slices (rows) into the blender, press blend, and get one single cup of juice (summary number). SUM gives the total weight, AVG gives average sweetness, and COUNT tells you how many slices went in.

Real-World Analogy

Think of a class test score sheet. COUNT(*) counts how many students are enrolled. COUNT(score) counts how many actually showed up to take the exam. AVG(score) computes the class average among test-takers only (ignoring absent students rather than giving them a 0 unless you use COALESCE).

Foundational Principles

Core Concept Breakdown

Essential database invariants, relational theory, and execution behavior.

1What an aggregate function returns

An aggregate function consumes values from multiple input rows and returns a summary value. COUNT counts rows or non-NULL values, SUM totals numeric values, AVG calculates a mean, and MIN/MAX find the smallest or largest value. Without GROUP BY, an aggregate usually summarizes the complete filtered input and produces one result row, even when the input is empty (with COUNT returning zero and most other aggregates returning NULL).

Free Preview Limit Reached (2/2 Lessons Used)

Sign In to Unlock Full Lesson Content

Visitors are limited to 2 free preview lessons. Create a free StudyVex account to unlock all 38 comprehensive theory chapters, runnable query walkthroughs, visual input & result tables, and interactive placement quizzes.