SQL · Queries that survive scale
How do COUNT, SUM, AVG, MIN, and MAX behave?
Short answer
Aggregates collapse many rows into one value. COUNT(*) counts rows, COUNT(column) counts non-NULL values, and SUM, AVG, MIN, and MAX ignore NULL inputs.
Aggregate functions operate on a set of rows and return one value. They are usually combined with GROUP BY, but without it the whole result set is one group.
SELECT
COUNT(*) AS all_rows,
COUNT(manager_id) AS rows_with_manager,
COUNT(DISTINCT department_id) AS departments,
AVG(salary) AS average_salary,
MAX(salary) AS top_salary
FROM employees;Important semantics
- COUNT(*) counts rows regardless of NULLs
- COUNT(column) and COUNT(DISTINCT column) exclude NULLs
- SUM and AVG ignore NULL inputs rather than treating them as zero
- AVG divides by the number of non-NULL values, not the total row count
- MIN and MAX return NULL when every input is NULL
- SUM over no rows returns NULL, while COUNT returns zero
If NULL should mean zero in a business calculation, use COALESCE explicitly. If the denominator matters, compute SUM and COUNT separately instead of relying on AVG.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between COUNT(*) and COUNT(column)?
- 2Does AVG ignore NULLs?
- 3How do you compute a conditional count?
- 4What does SUM return when there are no rows?
Say it in the interview
Aggregates reduce a set of rows to a single value. COUNT(*) counts rows, while COUNT(column) counts only non-NULL values. SUM, AVG, MIN, and MAX ignore NULLs, which affects the denominator of AVG. When NULL should mean zero, I use COALESCE explicitly, and for conditional counts I use CASE or FILTER rather than relying on accidental NULL behavior.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live SQL mock interview and get scored feedback on depth, clarity and confidence.