Lessons
SQL Constraints
SQL Data Handling
SQL SELECT and WHERE
Aggregate Functions
Nested Queries
Conclusion
SQL Aggregate Functions
What Are SQL Aggregate Functions?
SQL aggregate functions are used to perform calculations on a set of values and return a single summarized value. These functions are commonly used to analyze data, providing insights such as totals, averages, or counts.
Commonly Used SQL Aggregate Functions
MIN()
The MIN()
function is used to find the smallest value in a column. It works on both numerical and text data types.
Example: Find the youngest student in a Students
table.
1 2
SELECT MIN(Age) AS Youngest FROM Students;
Use: This helps identify the minimum value, such as the lowest price of a product or the earliest date in a column.
MAX()
The MAX()
function returns the largest value in a column. Like MIN()
, it works on numerical and text data types.
Example: Find the highest salary in an Employees
table.
1 2
SELECT MAX(Salary) AS HighestSalary FROM Employees;
Use: Useful for identifying the largest value, such as the highest score in a test or the most recent date in a column.
COUNT()
The COUNT()
function returns the total number of rows in a dataset or within a specific column. It is frequently used to count non-NULL values.
Example: Count the number of students enrolled in a Courses
table.
1 2
SELECT COUNT(StudentID) AS TotalStudents FROM Courses;
Use: Ideal for counting rows, such as the number of employees, students, or products in a table.
SUM()
The SUM()
function calculates the total sum of a numerical column.
Example: Find the total sales in an Orders
table.
1 2
SELECT SUM(OrderAmount) AS TotalSales FROM Orders;
Use: Useful for calculating totals, such as total revenue, total expenses, or total inventory value.
AVG()
The AVG()
function calculates the average of a numerical column.
Example: Find the average score in a Grades
table.
1 2
SELECT AVG(Score) AS AverageScore FROM Grades;
Use: Helpful for finding averages, such as average salaries, grades, or product ratings.
Key Takeaways
- Purpose: Aggregate functions are used to perform calculations on a set of values and return a single summarized value.
- Common Functions: The most frequently used aggregate functions include
MIN()
,MAX()
,COUNT()
,SUM()
, andAVG()
. - Applications: Aggregate functions are ideal for tasks like calculating totals, averages, counts, and finding maximum or minimum values.
- NULL Handling: Aggregate functions generally ignore
NULL
values unless explicitly handled.
Practical Uses of SQL Aggregate Functions
- Business Analytics: Calculate total revenue, average sales, or customer counts for specific regions or time periods.
- Education: Analyze average grades, count of students per course, or highest marks in exams.
- Inventory Management: Determine total stock levels, average inventory value, or maximum stock for a product.
- Time-Based Analysis: Find earliest or latest dates for events, count orders per day, or calculate averages over time periods.
- Quality Assessment: Calculate average product ratings, identify top-performing employees, or determine minimum response times.