SQL FOUNDATIONS:Aggregations (COUNT, SUM, AVG, MIN, MAX)

Summarize data with COUNT, SUM, AVG, MIN, and MAX.

SQL Aggregations (COUNT, SUM, AVG, MIN, MAX) Explained with Examples + Practice Questions

When to Use Aggregations

  • Use COUNT to count rows
  • Use SUM to calculate totals
  • Use AVG to find averages
  • Use MIN / MAX to find smallest or largest values

These are heavily used in:

  • Data analysis
  • Dashboards
  • SQL interviews

Example Dataset

Table: employees

idnamedepartmentsalary
1JohnHR40000
2AliceIT60000
3BobIT55000
4EmmaFinance70000
5DavidHR45000
6AnkitIT52000

COUNT Function

Counts number of rows.

sql
SELECT COUNT(*) FROM employees;

Counts all rows.

sql
SELECT COUNT(department) FROM employees;

Counts non-null values in a column.

SUM Function

Calculates total.

sql
SELECT SUM(salary) FROM employees;

AVG Function

Calculates average.

sql
SELECT AVG(salary) FROM employees;

MIN Function

Finds smallest value.

sql
SELECT MIN(salary) FROM employees;

MAX Function

Finds largest value.

sql
SELECT MAX(salary) FROM employees;

Using Aggregations with WHERE

sql
SELECT COUNT(*)
FROM employees
WHERE department = 'IT';

Counts employees in IT.

Combining Multiple Aggregations

sql
SELECT
COUNT(*) AS total_employees,
AVG(salary) AS avg_salary,
MAX(salary) AS highest_salary
FROM employees;

Key Concepts

  • Aggregations return a single value
  • Ignore NULL values (except COUNT(*))
  • Often combined with WHERE
  • Frequently used with GROUP BY (next chapter)

SQL Aggregation Functions Comparison

FunctionPurposeExample
COUNTCount rowsCOUNT(*)
SUMTotal valuesSUM(salary)
AVGAverage valueAVG(salary)
MINSmallest valueMIN(salary)
MAXLargest valueMAX(salary)

Common Questions (FAQ)

What is COUNT(*) in SQL

COUNT(*) counts all rows, including those with NULL values.

What is difference between COUNT(*) and COUNT(column)

COUNT(column) ignores NULL values, COUNT(*) counts all rows.

Does AVG include NULL values

No, NULL values are ignored.

Can we use multiple aggregation functions together

Yes, multiple aggregation functions can be used in one query.

Internal Learning Links

  • Learn SELECT basics
  • Learn WHERE filtering
  • Learn GROUP BY

Practice CTA

Practice aggregation-based SQL problems on Schoolabe to strengthen your analytical skills.

Aggregations (COUNT, SUM, AVG, MIN, MAX) Missions

Solve exercises in sequence to unlock the next mission.

1

Count employees

Count total number of employees.

Solve Mission
2

Count HR employees

Count employees in HR department.

Locked
3

Total salary

Find total salary of all employees.

Locked
4

Average salary

Find average salary.

Locked
5

Highest salary

Find highest salary.

Locked
6

Lowest salary

Find lowest salary.

Locked
7

Total IT salary

Find total salary of IT employees.

Locked
8

Average HR salary

Find average salary of HR employees.

Locked
9

Count salary > 50000

Count employees with salary greater than 50000.

Locked
10

Salary range

Find difference between highest and lowest salary.

Locked