Aggregate Functions — COUNT, SUM, AVG, MAX, MIN
Aggregate functions summarise data — used in almost every real analytics query. Critical for data analyst interviews.
How many employees are in the company?
SELECT COUNT(*) AS total_employees
FROM employees;COUNT(*) counts all rows including NULLs. COUNT(column) counts non-NULL values in that column. Always alias your aggregates for readable output.
What is the total salary paid by the company?
SELECT SUM(salary) AS total_salary
FROM employees;SUM ignores NULL values. If you need to treat NULLs as 0 use SUM(COALESCE(salary, 0)).
Find the average salary per department.
SELECT department,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department;When using aggregate functions with non-aggregate columns, every non-aggregate column must appear in GROUP BY.
Find the highest and lowest salary in each department.
SELECT department,
MAX(salary) AS highest,
MIN(salary) AS lowest
FROM employees
GROUP BY department;You can combine multiple aggregate functions in one query. This is a common reporting pattern for salary band analysis.
Show only departments where the average salary exceeds 50000.
SELECT department,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;HAVING filters after GROUP BY. You cannot use the alias avg_salary in the HAVING clause in most databases — repeat the expression.
Count employees in each department, show only departments with more than 10 employees.
SELECT department,
COUNT(*) AS emp_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 10
ORDER BY emp_count DESC;This combines GROUP BY + HAVING + ORDER BY — a pattern asked frequently in e-commerce and banking sector SQL interviews in Noida/Gurgaon.
EVIKA ACADEMY · SQL FOR DATA ANALYTICS
Want to master SQL with live practice?
Join our SQL for Data Analytics course — live classes in Noida and online across India.
Book Free Demo Class →