SQL FOUNDATIONS:Aggregations (COUNT, SUM, AVG, MIN, MAX)
Summarize data with COUNT, SUM, AVG, MIN, and MAX.
SQL Aggregations (COUNT, SUM, AVG, MIN, MAX) Explained with Examples + Practice Questions
When to Use Aggregations
- Use COUNT to count rows
- Use SUM to calculate totals
- Use AVG to find averages
- Use MIN / MAX to find smallest or largest values
These are heavily used in:
- Data analysis
- Dashboards
- SQL interviews
Example Dataset
Table: employees
COUNT Function
Counts number of rows.
sqlSELECT COUNT(*) FROM employees;
Counts all rows.
sqlSELECT COUNT(department) FROM employees;
Counts non-null values in a column.
SUM Function
Calculates total.
sqlSELECT SUM(salary) FROM employees;
AVG Function
Calculates average.
sqlSELECT AVG(salary) FROM employees;
MIN Function
Finds smallest value.
sqlSELECT MIN(salary) FROM employees;
MAX Function
Finds largest value.
sqlSELECT MAX(salary) FROM employees;
Using Aggregations with WHERE
sqlSELECT COUNT(*)FROM employeesWHERE department = 'IT';
Counts employees in IT.
Combining Multiple Aggregations
sqlSELECTCOUNT(*) AS total_employees,AVG(salary) AS avg_salary,MAX(salary) AS highest_salaryFROM employees;
Key Concepts
- Aggregations return a single value
- Ignore NULL values (except COUNT(*))
- Often combined with WHERE
- Frequently used with GROUP BY (next chapter)
SQL Aggregation Functions Comparison
Common Questions (FAQ)
What is COUNT(*) in SQL
COUNT(*) counts all rows, including those with NULL values.
What is difference between COUNT(*) and COUNT(column)
COUNT(column) ignores NULL values, COUNT(*) counts all rows.
Does AVG include NULL values
No, NULL values are ignored.
Can we use multiple aggregation functions together
Yes, multiple aggregation functions can be used in one query.
Internal Learning Links
- Learn SELECT basics
- Learn WHERE filtering
- Learn GROUP BY
Practice CTA
Practice aggregation-based SQL problems on Schoolabe to strengthen your analytical skills.
Aggregations (COUNT, SUM, AVG, MIN, MAX) Missions
Solve exercises in sequence to unlock the next mission.
Count HR employees
Count employees in HR department.
Total salary
Find total salary of all employees.
Average salary
Find average salary.
Highest salary
Find highest salary.
Lowest salary
Find lowest salary.
Total IT salary
Find total salary of IT employees.
Average HR salary
Find average salary of HR employees.
Count salary > 50000
Count employees with salary greater than 50000.
Salary range
Find difference between highest and lowest salary.