← 30 Days of SQL
Day 4 / 30Aggregations

Aggregate Functions — COUNT, SUM, AVG, MAX, MIN

Aggregate functions summarise data — used in almost every real analytics query. Critical for data analyst interviews.

1
Easy

How many employees are in the company?

SQL Answer
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.

2
Easy

What is the total salary paid by the company?

SQL Answer
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)).

3
Easy

Find the average salary per department.

SQL Answer
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.

4
Easy

Find the highest and lowest salary in each department.

SQL Answer
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.

5
Medium

Show only departments where the average salary exceeds 50000.

SQL Answer
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.

6
Medium

Count employees in each department, show only departments with more than 10 employees.

SQL Answer
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 →
← PREVIOUSDay 3: LIKE, IN, BETWEEN and IS NULLNEXT →Day 5: GROUP BY Deep Dive
Best Data Analytics Course in Noida Delhi NCR | EVIKA ACADEMY