SQL Aggregate Functions
Definition
SQL aggregate functions perform calculations on a set of rows and return a single summary value, including COUNT, SUM, AVG, MIN, and MAX.
Introduction to SQL Aggregate Functions
Aggregate functions are the mathematical backbone of SQL analytics. They transform a set of rows into a single computed value — the count, total, average, smallest, or largest. Combined with GROUP BY, they power business reporting, dashboards, and data analysis.
Syntax
SELECT COUNT(*) AS total_rows, COUNT(DISTINCT col) AS unique_vals, SUM(col) AS total, AVG(col) AS average, MIN(col) AS minimum, MAX(col) AS maximum FROM table GROUP BY group_col;
Examples
Sales Summary Dashboard
SELECT department, COUNT(*) AS employees, SUM(salary) AS total_payroll, AVG(salary) AS avg_salary, MIN(salary) AS min_salary, MAX(salary) AS max_salary FROM employees GROUP BY department;
A classic salary analytics query using all five aggregate functions in one GROUP BY query.
Try in PlaygroundCOUNT DISTINCT — Unique Values
SELECT COUNT(*) AS total_orders, COUNT(DISTINCT customer_id) AS unique_customers, COUNT(DISTINCT product_id) AS unique_products FROM orders;
COUNT(*) counts all rows. COUNT(DISTINCT col) counts only unique non-NULL values. Essential for customer analytics.
Try in PlaygroundCommon Mistakes
COUNT(*) vs COUNT(col) — COUNT(*) counts all rows including NULLs; COUNT(col) skips NULL values
AVG ignores NULL values — this can skew averages if NULLs represent zero
Using aggregate functions in WHERE instead of HAVING
SUM on non-numeric columns — always verify the column data type
Frequently Asked Questions
What is the difference between COUNT(*) and COUNT(column)?
Does AVG ignore NULL values in SQL?
Can aggregate functions be nested in SQL?
Related SQL Topics
Practice This in the SQL Playground
Write real queries, see live results, and master SQL Aggregate Functions hands-on. 100% free.