An aggregate function takes many rows and returns one value calculated from them. SQL has five you will use every day: COUNT counts rows, SUM adds a column, AVG averages it, and MIN and MAX return the smallest and largest value. On their own they summarise the whole table; with GROUP BY they return one summary per group.
The syntax takes ten minutes to learn. What takes longer is knowing how each one treats NULL, because that is where reports quietly go wrong: COUNT(*) and COUNT(column) give different answers on the same table, and AVG skips blanks in a way that can move your number by a third.
The five core aggregate functions
The table gives each function, what it returns, its syntax, and an example you can run against the loan book introduced below.
| Function | What it returns | Syntax | Example |
|---|---|---|---|
| COUNT | Number of rows, or of non-NULL values in a column | COUNT(*) or COUNT(column) | COUNT(*) FROM loans |
| SUM | Total of a numeric column | SUM(column) | SUM(amount) FROM loans |
| AVG | Arithmetic mean of a numeric column | AVG(column) | AVG(amount) FROM loans |
| MIN | Smallest value; works on numbers, dates and text | MIN(column) | MIN(amount) FROM loans |
| MAX | Largest value; works on numbers, dates and text | MAX(column) | MAX(disbursed_on) FROM loans |
Every one of them, except COUNT(*), ignores NULL. Keep that sentence in mind for the rest of the article.
The data used throughout
The examples use a small loan book from a Pune lender: seven loans across three branches. Amounts are in rupees. Two borrowers are new to credit and have no bureau score yet, so credit_score is NULL for them.
| loan_id | branch | product | amount | credit_score |
|---|---|---|---|---|
| L01 | Kothrud | Personal | 200000 | 742 |
| L02 | Kothrud | Gold | 80000 | NULL |
| L03 | Hinjewadi | Personal | 350000 | 781 |
| L04 | Hinjewadi | Two-wheeler | 95000 | 705 |
| L05 | Hinjewadi | Personal | 150000 | NULL |
| L06 | Baner | Gold | 60000 | 688 |
| L07 | Baner | Personal | 275000 | 760 |
COUNT: three versions, three answers
COUNT has three forms, and on this table they return three different numbers.
Query: SELECT COUNT(*) AS all_loans, COUNT(credit_score) AS scored_loans, COUNT(DISTINCT branch) AS branches, COUNT(DISTINCT product) AS products FROM loans
| all_loans | scored_loans | branches | products |
|---|---|---|---|
| 7 | 5 | 3 | 3 |
COUNT(*) counts rows, whatever is in them, so it returns 7. COUNT(credit_score) counts rows where credit_score is not NULL, so it returns 5. COUNT(DISTINCT branch) counts unique values, ignoring NULL, so it returns 3.
The practical rule: use COUNT(*) for “how many records”, COUNT(column) for “how many records have this filled in”, and COUNT(DISTINCT column) for “how many different ones”. COUNT(1) is identical to COUNT(*) in every major database; the belief that it is faster is a myth that refuses to die.
SUM, AVG, MIN and MAX on the whole table
Query: SELECT SUM(amount) AS total_book, AVG(amount) AS avg_loan, MIN(amount) AS smallest, MAX(amount) AS largest FROM loans
| total_book | avg_loan | smallest | largest |
|---|---|---|---|
| 1210000 | 172857.14 | 60000 | 350000 |
The lender has 12,10,000 rupees on the book, an average ticket of about 1,72,857, and loans ranging from 60,000 to 3,50,000. Nothing surprising here, because amount has no NULLs. The interesting case is the column that does.
How aggregates treat NULL, and why AVG can mislead
Now average the credit score.
Query: SELECT AVG(credit_score) AS avg_score, SUM(credit_score) * 1.0 / COUNT(*) AS score_over_all_rows, AVG(COALESCE(credit_score, 0)) AS nulls_as_zero FROM loans
| avg_score | score_over_all_rows | nulls_as_zero |
|---|---|---|
| 735.2 | 525.14 | 525.14 |
AVG(credit_score) skipped the two NULLs and averaged the five real scores: 3,676 divided by 5 gives 735.2. Dividing the same sum by all seven rows, or treating NULL as zero, gives 525.14, which would make this look like a sub-prime book. (The 1.0 forces decimal division; without it, PostgreSQL and SQL Server divide two integers and return 525.)
For a credit score, 735.2 is the right answer, because a missing score is not a score of zero. For something like “rupees collected per loan this month”, a NULL may genuinely mean nothing was collected, and then COALESCE to 0 is right. The function cannot know which you mean. You have to decide, and it is worth writing the decision into a comment in the query.
Two more NULL behaviours to know:
- SUM, AVG, MIN and MAX of zero rows return NULL, not 0. SUM(amount) WHERE branch = ‘Wakad’ returns NULL because there is no Wakad branch. Wrap it in COALESCE(SUM(amount), 0) if a dashboard expects a number.
- COUNT never returns NULL. On zero rows it returns 0.
Aggregates with GROUP BY
Add GROUP BY and each aggregate runs once per group. This is how nearly every summary table in a report is built.
Query: SELECT branch, COUNT(*) AS loans, SUM(amount) AS book, ROUND(AVG(amount), 0) AS avg_ticket, MAX(amount) AS largest, COUNT(credit_score) AS scored FROM loans GROUP BY branch ORDER BY book DESC
| branch | loans | book | avg_ticket | largest | scored |
|---|---|---|---|---|---|
| Hinjewadi | 3 | 595000 | 198333 | 350000 | 2 |
| Baner | 2 | 335000 | 167500 | 275000 | 2 |
| Kothrud | 2 | 280000 | 140000 | 200000 | 1 |
Hinjewadi carries nearly half the book. The rules for which columns may appear next to an aggregate, and how to filter groups with HAVING, are a lesson of their own; this article stays with the functions themselves.
Conditional aggregation: several answers in one pass
Putting a CASE expression inside an aggregate lets one query answer several questions that would otherwise need several queries. It is the most useful aggregate pattern most beginners have never seen.
| Question | Expression |
|---|---|
| Personal-loan book | SUM(CASE WHEN product = ‘Personal’ THEN amount ELSE 0 END) |
| Number of personal loans | COUNT(CASE WHEN product = ‘Personal’ THEN 1 END) |
| Personal share of the book, % | ROUND(100.0 * SUM(CASE WHEN product = ‘Personal’ THEN amount ELSE 0 END) / SUM(amount), 1) |
| Loans with a score under 700 | COUNT(CASE WHEN credit_score < 700 THEN 1 END) |
Run all four in one SELECT against the loans table:
| personal_book | personal_loans | personal_share_pct | under_700 |
|---|---|---|---|
| 975000 | 4 | 80.6 | 1 |
The COUNT version works because CASE with no ELSE returns NULL for non-matching rows, and COUNT skips NULLs. The under-700 count is 1, not 3: the two unscored loans are NULL, NULL is not less than 700, so they are not counted. That is usually what you want, and it is another place to be deliberate.
PostgreSQL has a neater form for the same thing: COUNT(*) FILTER (WHERE product = ‘Personal’). MySQL and SQL Server do not support FILTER, so the CASE form is the portable one to learn first.
Less common aggregates worth knowing
These come up in analyst work less often, but they come up.
| Function | What it does | Example |
|---|---|---|
| STRING_AGG / GROUP_CONCAT | Joins text values from a group into one string | STRING_AGG(loan_id, ‘, ‘) returns L03, L04, L05 for Hinjewadi |
| STDDEV / STDEV | Standard deviation of a column | STDDEV(amount) in MySQL and PostgreSQL, STDEV(amount) in SQL Server |
| VARIANCE / VAR | Variance of a column | VARIANCE(amount) in MySQL and PostgreSQL, VAR(amount) in SQL Server |
| SUM(DISTINCT col) | Total of distinct values only | Rarely right; two loans of the same amount get counted once |
Where MySQL, PostgreSQL and SQL Server differ
The five core functions behave the same everywhere with one important exception: what AVG returns for a whole-number column.
| Point | MySQL | PostgreSQL | SQL Server |
|---|---|---|---|
| AVG of an INT column (scores 742, 781, 705, 688, 760) | 735.2000 | 735.2000000000000000 | 735, truncated to an integer |
| Join text in a group | GROUP_CONCAT(loan_id SEPARATOR ‘, ‘) | STRING_AGG(loan_id, ‘, ‘) | STRING_AGG(loan_id, ‘, ‘) from SQL Server 2017 |
| Order inside the joined text | GROUP_CONCAT(loan_id ORDER BY loan_id SEPARATOR ‘, ‘) | STRING_AGG(loan_id, ‘, ‘ ORDER BY loan_id) | STRING_AGG(loan_id, ‘, ‘) WITHIN GROUP (ORDER BY loan_id) |
| FILTER clause | No | Yes | No |
| Standard deviation | STDDEV or STD | STDDEV | STDEV |
The SQL Server row is the one that costs people. AVG on an integer column returns an integer there, so 735.2 silently becomes 735, and an average rating of 4.6 stars becomes 4. Write AVG(CAST(credit_score AS DECIMAL(10,2))), or AVG(credit_score * 1.0), when the column is an integer type.
Mistakes that produce wrong numbers
Counting a column when you meant rows. COUNT(credit_score) told us 5 loans when there are 7. If a count looks low, check whether you counted a column with blanks.
Treating NULL as zero by accident, or failing to when you should. AVG skips NULL; decide whether that is right for the metric before it goes on a slide.
Averaging averages. The average of the three branch averages above is about 1,68,611. The true average loan is 1,72,857. Branches with more loans should weigh more, so compute the average from the raw rows, or as SUM divided by COUNT across groups.
Summing after a join that multiplied rows. Join loans to a repayments table with several rows per loan, then SUM(amount), and each loan is counted once per repayment. Aggregate before you join, or our SQL joins guide explains how to spot this fan-out.
Mixing aggregated and plain columns. SELECT branch, SUM(amount) FROM loans without GROUP BY branch errors in PostgreSQL and SQL Server, and in MySQL with its default settings.
Aggregates are tested in the first round of almost every analyst interview, usually through COUNT(*) versus COUNT(column) and the NULL questions above; our SQL interview questions cover how they are phrased. If you already summarise data with pivot tables, the logic is the same as pivot tables in Excel, just written as a query.
Frequently asked questions
What are aggregate functions in SQL?
Aggregate functions calculate a single value from a set of rows. The five standard ones are COUNT, SUM, AVG, MIN and MAX. Used alone they summarise the whole table, and used with GROUP BY they return one summary value per group, such as total sales per city.
What is the difference between COUNT(*) and COUNT(column)?
COUNT(*) counts every row in the result, including rows where some columns are NULL. COUNT(column) counts only the rows where that column has a value. On a table of seven loans where two have no credit score, COUNT(*) returns 7 and COUNT(credit_score) returns 5.
Do aggregate functions ignore NULL values?
Yes, all of them except COUNT(*). SUM, AVG, MIN, MAX and COUNT(column) skip NULLs. This means AVG divides by the number of non-NULL values, not by the number of rows. If NULL should count as zero for your metric, wrap the column in COALESCE(column, 0) first.
Can I use an aggregate function in a WHERE clause?
No. WHERE filters individual rows before any aggregation happens, so the aggregate value does not exist yet. To filter on an aggregate, such as branches with a book above 3 lakh, use HAVING after GROUP BY, or compute the aggregate in a subquery or CTE and filter on it outside.
Is COUNT(1) faster than COUNT(*)?
No. In MySQL, PostgreSQL, SQL Server and Oracle, COUNT(1) and COUNT(*) produce the same execution plan and run at the same speed. COUNT(*) is the clearer choice because it says what it means: count the rows.
Why does AVG return a whole number in SQL Server?
Because SQL Server returns the average in the same data type as the input column, so AVG of an INT column is an INT and the decimals are truncated. Cast the column first, for example AVG(CAST(rating AS DECIMAL(10,2))), or multiply it by 1.0 inside the AVG. MySQL and PostgreSQL return a decimal for the same query.
Turn summaries into analysis
Aggregates are the first thing every analyst writes and the place most wrong numbers start. The skill is less in the syntax than in asking what a NULL means and whether a join has multiplied your rows.
SkilloVilla’s SQL: Beginner to Advanced course has a full module on aggregation functions and GROUP BY, alongside joins, conditional logic and advanced SQL statements, taught in live classes with mentor support and practice on SQL playgrounds. For SQL as part of a complete analyst programme, see the Data Analytics with Python track.