SQL Aggregate
In real life, we rarely look at data row by row. Most of the time, we ask questions like:
- How many employees are there?
- What is the total salary paid?
- What is the average marks of students?
- What is the highest or lowest value?
These questions are not about individual records, but about summarizing data. To perform such calculations on a group of rows, SQL provides Aggregate Functions.
What Are Aggregate Functions?
Aggregate functions in SQL perform calculations on multiple rows and return a single summarized value.
They help convert large datasets into meaningful insights.
Aggregate functions are commonly used with:
- SELECT statement
- GROUP BY clause
- HAVING clause
Common Aggregate Functions in SQL
| Function | Purpose |
|---|---|
| COUNT ( ) | Counts number of rows |
| SUM ( ) | Calculates total value |
| AVG ( ) | Calculates average |
| MIN ( ) | Finds minimum value |
| MAX ( ) | Finds maximum value |
Example: employees Table
| id | name | salary | department |
|---|---|---|---|
| 1 | Amit | 60000 | IT |
| 2 | Neha | 75000 | HR |
| 3 | Rahul | 50000 | IT |
| 4 | Priya | 90000 | Finance |
| 5 | Karan | 45000 | HR |
1. COUNT( ) Function
Why COUNT( ) Is Needed
Often, we need to know how many records exist in a table or match a condition.
Examples
- Total number of employees
- Number of students who passed
- Number of orders placed
What COUNT( ) Does
COUNT( ) returns the number of rows in a result set.
Syntax
COUNT(column_name)Example: Count Total Employess
SELECT COUNT(*) AS total_employeesFROM employees;Returns the total number of rows in the table.
2. SUM( ) Function
Why SUM( ) Is Needed
When working with numeric data, we often need to calculate totals.
Examples
- Total salary paid by a company
- Total sales amount
- Total marks scored
What SUM( ) Does
SUM( ) returns the total of a numeric column.
Syntax
SUM(column_name)Example: Total Salary of All Employees
SELECT SUM(salary) AS total_salaryFROM employees;3. AVG( ) Function
Why AVG( ) Is Needed
Average values help in
- Performance analysis
- Decision making
- Comparing data
What AVG( ) Does
AVG( ) calculates the average value of a numeric column.
Syntax
AVG(column_name)Example: Average Employee Salary
SELECT AVG(salary) AS average_salaryFROM employees;4. MIN( ) Function
Why MIN( ) Is Needed
To find the lowest value in a dataset.
Examples
- Lowest salary
- Minimum marks
- Cheapest product price
What MIN( ) Does
MIN( ) returns the smallest value from a column.
Example
SELECT MIN(salary) AS lowest_salaryFROM employees;5. MAX( ) Function
Why MAX( ) Is Needed
To find the highest value in a dataset.
Examples
- Highest salary
- Maximum marks
- Most expensive product
What MAX( ) Does
MAX( ) returns the largest value from a column.
Example
SELECT MAX(salary) AS highest_salaryFROM employees;Using Aggregate Functions with WHERE Clause
The WHERE clause filters rows before aggregation.
Example: Average Salary of IT Departement
SELECT AVG(salary)FROM employeesWHERE department = 'IT';Important Points About Aggregate Functions
- Aggregate functions return one value
- They ignore NULL values (except COUNT(*))
- Mostly used with GROUP BY
- Cannot be directly used in WHERE clause
- Useful for reports and analytics
Two Minute Drill
- Aggreate functions summarize data
- COUNT ( ) counts rows
- SUM ( ) adds numeric values
- AVG ( ) calculates average
- MIN ( ) finds smallest value
- MAX ( ) finds largest value
- Used for reports and analysis