
For an instructor lead, in-depth look at learning SQL click below.
SQL (Structured Query Language) is a powerful tool used to manipulate and analyze data stored in relational databases. SQL has a set of powerful functions known as “aggregate functions” that perform calculations on a set of values and return a single value. These functions include SUM, AVG, MAX and MIN. In this post, we will discuss these functions and look at examples of how to use them with the most common SQL databases: MySQL, SQL Server, and Oracle.
1. SUM Function
In SQL, the SUM function is used to add up all the values in a particular column.
1 2 3 4 |
SELECT SUM(salary) AS "Total Salary" FROM employees; |
In this example, the SQL server will return the total of all salaries from the employees table.
2. AVG Function
The AVG function calculates the average of a set of values in a particular column.
1 2 3 4 |
SELECT AVG(salary) AS "Average Salary" FROM employees; |
Here, the SQL server will return the average salary from the employees table.
3. MAX Function
The MAX function is used to find the maximum value in a set of values.
1 2 3 4 |
SELECT MAX(salary) AS "Max Salary" FROM employees; |
This code will return the highest salary from the employees table.
4. MIN Function
Just as the MAX function finds the maximum value, the MIN function finds the minimum value in a set of values.
1 2 3 4 |
SELECT MIN(salary) AS "Min Salary" FROM employees; |
This will return the lowest salary from the employees table.
Conclusion
By understanding these SQL aggregate functions, you can perform data calculations directly within your SQL queries, making it a powerful tool for analysis and reporting. Practice with different datasets and functions to get familiar with their usefulness and flexibility. Happy querying!