Quick answer
SQL aggregate functions summarize data across multiple rows and return a single value.
The most common ones are:
COUNT()→ how many rowsSUM()→ total valueAVG()→ average valueMIN()→ smallest valueMAX()→ largest value
In this guide
Sample table
Imagine this employees table:
| employee_id | name | department | salary | city |
|---|---|---|---|---|
| 1 | Ana | Sales | 62000 | Chicago |
| 2 | Ben | IT | 78000 | Dallas |
| 3 | Chris | HR | 56000 | Chicago |
| 4 | Dana | IT | 85000 | Denver |
| 5 | Evan | Sales | 59000 | Austin |
COUNT()
COUNT() tells you how many rows exist.
SELECT COUNT(*) AS total_employees FROM employees;
This returns the total number of rows in the table.
COUNT(*) counts all rows, even if some columns contain NULL values.
SUM()
SUM() adds up numeric values.
SELECT SUM(salary) AS total_salary FROM employees;
This returns the total salary across all employees.
SUM() for totals like revenue, expenses, quantities, or salaries.
AVG()
AVG() calculates the average value.
SELECT AVG(salary) AS avg_salary FROM employees;
This returns the average salary.
AVG() ignores NULL values when calculating the average.
MIN() and MAX()
MIN() returns the smallest value.
MAX() returns the largest value.
SELECT MIN(salary) AS lowest_salary,
MAX(salary) AS highest_salary
FROM employees;
This shows the lowest and highest salaries in the table.
Using aggregate functions with GROUP BY
Aggregate functions become much more powerful when combined with GROUP BY.
SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department;
This returns one row per department with a count of employees.
How NULL values behave
COUNT(*)counts all rowsCOUNT(column)ignores NULL valuesSUM()andAVG()ignore NULL values
When to use aggregate functions
Common mistakes
- Using aggregate functions without understanding GROUP BY
- Mixing normal columns and aggregates without grouping properly
- Forgetting that NULL values are ignored
- Using COUNT(column) and expecting it to count NULL rows
Practice questions
Bottom line
Aggregate functions let you summarize data quickly and answer high-level questions.
Related SQL tutorials
Start here for all SQL tutorials.
Learn how to group rows before applying aggregate functions.
Learn how to combine tables before summarizing data.
Learn how to retrieve data before summarizing it.