Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9
Aggregate Functions in SQL
Aggregate functions in SQL are used to perform operations on multiple rows of
a table and return a single value. A single function can be used to compute multiple values and return the required result. There are 5 types of SQL aggregate functions: Count() Sum() Avg() Min() Max() StudentID Name Score 1 Alice 85 2 Bob 92 3 Charlie 78 4 David 65 5 Eva Null 6 Frank 90 7 George 92 8 Helen 78 Count(): o Count(*): Provides the total number of records in the dataset, which is 8. o Count(Score): Presents the count of non-null values in the Score column, indicating there are 7 such values. o Count(Distinct Score): Indicates the count of distinct non-null values in the Score column, totaling 5 unique scores. Sum(): o sum(Score): Aggregates the sum of all non-null values in the Score column, resulting in a sum of 490. o sum(Distinct Score): Computes the sum of distinct non-null values in the Score column, amounting to 423 (85+78+90+92+65). Avg(): o Avg(Score): Computes the average score by dividing the sum of all scores by the count of non-null scores, resulting in 490 / 7 . o Avg(Distinct Score): Calculates the average of distinct scores by dividing the sum of distinct scores by the count of distinct scores, yielding approximately 423 / 5. Min(): o Min(Score): Identifies the minimum score in the Score column, excluding null values, which is 65. Max(): o Max(Score): Identifies the maximum score in the Score column, which is 92. Example
Employee_ID Name Department Salary
1 Ram Marketing 80000 The table stores 2 Henry Production 76000 a unique EMPLOYEE_ID, 3 Disha R&D 76000 Name of the 4 Helen R&D 84000 employee, department in which the 5 Meera Marketing 80000 employee is working, 6 Ashish Production 64000 and the salary. 1. COUNT() 7 Bob Production 60000 Function 8 Hari R&D 60000 The COUNT() aggreg 9 Preeti Marketing NULL ate function returns
10 Mark Production 66000
the total number of rows from a database table that matches the defined criteria in the SQL query. Syntax: COUNT(*) OR COUNT(COLUMN_NAME) COUNT(*) returns the total number of rows in a given table. COUNT(COULUMN_NAME) returns the total number of non-null values present in the column which is passed as an argument in the function. Example: Suppose you want to know the total number of employees working in the organization. You can do so by the below-given query. SELECT COUNT(*) FROM EMP_DATA; As COUNT(*) returns the total number of rows and the table named EMP_DATA provided above consists of 10 rows, so the COUNT(*) function returns 10. The output is printed as shown below. Output: COUNT(*) 10 Note: Except for COUNT(*), all other SQL aggregate functions ignore NULL values. Suppose you need to count the number of people who are getting a salary. The query given below can help you achieve this. SELECT COUNT(Salary) FROM EMP_DATA; Here, the Salary column is passed as a parameter to the COUNT() function, and hence, this query returns the number of non NULL values from the column Salary, i.e. 9. Output: COUNT(Salary) 9 Suppose you need to count the number of distinct departments present in the organization. The following query can help you achieve this. SELECT COUNT(DISTINCT Department) FROM EMP_DATA; The above query returns the total number of distinct non NULL values over the column Department i.e. 3 (Marketing, Production, R&D). The DISTINCT keyword makes sure that only non-repetitive values are counted. Output: COUNT(DISTINCT Department) 3 What if you want to calculate the number of people whose salaries are more than a given amount(say 70,000)? Check out the example below. SELECT COUNT(Salary) WHERE Salary >= 70000 FROM EMP_DATA; The query returns the number of rows where the salary of the employee is greater than or equal to 70,000 i.e 5. Output: COUNT(Salary) 5 2. SUM() Function The SUM() function takes the name of the column as an argument and returns the sum of all the non NULL values in that column. It works only on numeric fields(i.e the columns contain only numeric values). When applied to columns containing both non-numeric(ex - strings) and numeric values, only numeric values are considered. If no numeric values are present, the function returns 0. Syntax: The function name is SUM() and the name of the column to be considered is passed as an argument to the function. SUM(COLUMN_NAME) Let’s look into some examples to understand the usage better. Example: Suppose you need to build a budget for the organization and you need to know the total amount needed to provide salary to all the employees. To calculate the sum of all the values present in column Salary. You can refer to the below-given example. SELECT SUM(Salary) FROM EMP_DATA; The above mentioned query returns the sum of all non NULL values over the column Salary i.e 80000 + 76000 + 76000 + 84000 + 80000 + 64000 + 60000 + 60000 + 66000 = 646000 Output: SUM(Salary) 646000 What if you need to consider only distinct salaries? The following query will help you achieve that. SELECT SUM(DISTINCT Salary) FROM EMP_DATA; The DISTINCT keyword makes sure that only non-repetitive values are considered. The query returns the sum of all distinct non NULL values over the column Salary i.e. 80000 + 76000 + 84000 + 64000 + 60000 + 66000 = 430000 Output: SUM(DISTINCT Salary) 430000 Suppose you need to know the collective salaries for each department(say Marketing). The query given below can help you achieve this. SELECT SUM(SALARY) FROM EMP_DATA WHERE Department = "Marketing"; The query returns the sum of salaries of employees who are working in the Marketing Department i.e 80000 + 80000 = 160000 Output: SUM(Salary) 160000 Note: There are 3 rows consisting of Marketing as Department value but the third value is a NULL value. Thus, the sum is returned considering only the first two entries having Marketing as Department. 3. AVG() Function The AVG() aggregate function uses the name of the column as an argument and returns the average of all the non NULL values in that column. It works only on numeric fields(i.e the columns contain only numeric values). Note: When applied to columns containing both non-numeric(ex - strings) and numeric values, only numeric values are considered. If no numeric values are present, the function returns 0. Syntax: The function name is AVG() and the name of the column to be considered is passed as an argument to the function. AVG(COLUMN_NAME) Let's take a look at some examples to understand the function better. Example: To obtain the average salary of an employee of an organization, the following query can be used. SELECT AVG(Salary) FROM EMP_DATA; Here, the column name Salary is passed as an argument and thus the values present in column Salary are considered. The above query returns the average of all non NULL values present in the Salary column of the table. Average = (80000 + 76000 + 76000 + 84000 + 80000 + 64000 + 60000 + 60000 + 66000 ) / 9 = 646000 / 9 = 71777.77777 Output: AVG(Salary) 71777.77777 If you need to consider only distinct salaries, the following query will help you out. SELECT AVG(DISTINCT Salary) FROM EMP_DATA; The query returns the average of all non NULL distinct values present in the Salary column of the table. Average = (80000 + 76000 + 84000 + 64000 + 60000 + 66000) / 6 = 430000/ 6 = 71666.66666 Output: AVG(DISTINCT Salary) 71666.66666 4. MIN() Function The MIN() function takes the name of the column as an argument and returns the minimum value present in the column. MIN() returns NULL when no row is selected. Syntax: The function name is MIN() and the name of the column to be considered is passed as an argument to the function. MIN(COLUMN_NAME) To understand this better, let’s take a look at some examples. Example: Suppose you want to find out what is the minimum salary that is provided by the organization. The MIN() function can be used here with the column name as an argument. SELECT MIN(Salary) FROM EMP_DATA; The query returns the minimum value of all the values present in the mentioned column i.e 60000. Output: MIN(Salary) 60000 Suppose you need to know the minimum salary of an employee belonging to the Production department. The following query will help you achieve that. SELECT MIN(Salary) FROM EMP_DATA WHERE Department = "Production"; The query returns the minimum value of all the values present in the mentioned column and has Production as Department value i.e 60000. Output: MIN(Salary) 60000 5. MAX() Function in SQL The MAX() function takes the name of the column as an argument and returns the maximum value present in the column. MAX() returns NULL when no row is selected. Syntax: The function name is MAX() and the name of the column to be considered is passed as an argument to the function. MAX(COLUMN_NAME) To get a better idea how the function works, let’s look at some examples. Example: Suppose you want to find out what is the maximum salary that is provided by the organization. The MAX() function can be used here with the column name as an argument. SELECT MAX(Salary) FROM EMP_DATA; The query returns the maximum value of all the values present in the mentioned column i.e 84000. Output: MAX(Salary) 84000 Suppose you need to know the maximum salary of an employee belonging to the R&D department. The following query will help you achieve that. SELECT MAX(Salary) FROM EMP_DATA WHERE Department="R&D"; The query returns the maximum value of all the values present in the mentioned column and has R&D as Department value i.e 84000. MAX(Salary) 84000
Why Use Aggregate Functions?
Aggregate functions in SQL play a pivotal role in database management systems, enabling swift and efficient calculations on extensive datasets. They facilitate tasks such as generating statistical reports, conducting financial analysis, and managing inventory levels. Additionally, aggregate functions in SQL aid in comprehending data by simplifying tasks such as calculating average prices or total sales for specific periods, tasks that would otherwise be laborious and error-prone without them. In essence, aggregate functions in SQL are indispensable tools for individuals handling substantial data volumes, allowing them to extract valuable insights effectively.