0% found this document useful (0 votes)
27 views6 pages

TOPICMIDTERM

The document discusses several SQL aggregate functions including COUNT, AVG, SUM, MIN, and MAX. It provides the syntax and examples of using each function alone and with clauses like GROUP BY and HAVING. The examples use data from a sample salary table to demonstrate calculating metrics like average salary, total salary by employee, and minimum salary amount.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

TOPICMIDTERM

The document discusses several SQL aggregate functions including COUNT, AVG, SUM, MIN, and MAX. It provides the syntax and examples of using each function alone and with clauses like GROUP BY and HAVING. The examples use data from a sample salary table to demonstrate calculating metrics like average salary, total salary by employee, and minimum salary amount.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction to SQL Aggregate Functions

An aggregate function allows you to perform a calculation on a set of values to return


a single scalar value. We often use aggregate functions with
the GROUP BY and HAVING clauses of the SELECT statement.

The following are the most commonly used


SQL aggregate functions: AVG — calculates the average of a set of values.

COUNT — counts rows in a specified table or view.

MIN — gets the minimum value in a set of values.

MAX — gets the maximum value in a set of values.

SUM — calculates the sum of values.

Notice that all aggregate functions above ignore NULL values except for the COUNT function.

SQL aggregate functions syntax


To call an aggregate function, you use the following syntax: aggregate _ function (DISTINCT I ALL expression)

First, specify an aggregate function that you want to use e.g., MIN, MAX, AVG, SUM or COUNT.

Second, put DISTINCT or ALL modifier followed by an expression inside parentheses. If you explicitly use the DISTINCT
modifier, the aggregate function ignores duplicate values and only consider the unique values. If you use the ALL
modifier, the aggregate function uses all values for calculation or evaluation. The ALL modifier is used by default if you
do not specify any modifier explicitly.

SQL aggregate function examples

For this lesson, we will be using this sample table:

Database name : midterm

Table name : salarytable


COUNT Function
The COUNT() function returns the number of rows in a group.

The first form of the COUNT()function is as follows: 1.COUNT(*) - The COUNT(*) function returns a number of rows
in a specified table or view that includes the number of
duplicates and NULL values.

2. COUNT(DISTINCT column) - To return the number of rows


that excludes the number of duplicates and NULL values.

3. COUNT(ALL column) - The following table illustrates all


forms of the COUNT() function.

The following table illustrates all forms of the COUNT() function:

Simple SQL exampleTo get the number of records in the salary table, you use the COUNT(*) function as follows:

SELECT COUNT(*)FROM salarytable --------------> COUNT(*)

30

The pending salary is the salary whose amount is NULL. To get the number ofpending salaries, you use the following
query:

SELECT COUNT(*) 'Pending Salary' FROM salarytable WHERE SalaryAmount IS NULL-------------- Pending Salary

SQL COUNT(*) with GROUP BY clause example


To get the number of salaries by employees, you use the COUNT(*) function with the GROUP BY clause as the following
query:

SELECT Employee_LastName, COUNT(*) FROM salarytable GROUP BY Employee_LastName ORDER BY COUNT(*) DESC

The GROUP BY clause is used to group the orders by employees. For each group, the COUNT(*) function counts the
orders by customer.

SQL COUNT(*) with HAVING clause example


To get employees who have more than 3 orders, you use the COUNT(*) function with GROUP BY and HAVING clauses as
the following query:

SELECT Employee_LastName, COUNT(*) FROM salarytable GROUP BY Employee_LastName HAVING COUNT (*)>3

The GROUP BY clause divides the salaries into groups by employee_lastname. The COUNT(*) function returns the
number of salaries for each lastname. The HAVING clause gets only groups that have more than 3 salaries.
SQL COUNT ALL example
The following query returns the number of salary amount except for the NULL values:

---- COUNT(ALL SalaryAmount)

23

SELECT COUNT(ALL SalaryAmount) FROM salarytable;

SQL COUNT DISTINCT example


To exclude both NULL values and duplicates, you use the COUNT(DISTINCT column) as the following query:

SELECT COUNT(DISTINCT Employee_LastName) FROM salarytable;

---- COUNT(DISTINCT Employee_LastName)

10

AVG Function
AVG function

The AVG function returns a single value whose data type is determined by the
type of the result of the expression. The returned data type
could be any numeric type such as integer, float, etc.

Behind the scenes, the AVG function calculates the average of


values by dividing the total of these values by the number of
values except for the NULL values. Therefore, if the total of
those values exceeds the maximum value of data type of the
result, the database server will issue an error.

Note : The AVG function ignores NULL values.

Simple SQL AVG function example


The following query calculates the average salary amount in the salary table.

SELECT AVG(SalaryAmount) FROM salarytable; AVG(SalaryAmount)

-------17408.695652

To calculate the average of distinct salary amount, you can use the DISTINCT modifier in the AVG() function as the
following query:

SELECT AVG(DISTINCT SalaryAmount) FROM salarytable; AVG(DISTINCT SalaryAmount)

-----------------17483.333333

SQL AVG function with GROUP BY clause


To find the average of salaries for each employees, you can use the AVG function with the GROUP BY clause as the
following query:

SELECT Employee_LastName, Employee_FirstName, CAST(AVG(SalaryAmount) AS DOUBLE(7,2)FROM salarytable


GROUP BY Employee_LastName;
SQL AVG function with HAVING clause
To get the employee that has an average salary greater than 18000, you use the AVG function with GROUP BY and
HAVING clauses as the following query:

SELECT Employee_LastName, Employee _ FirstName, AVG(SalaryAmount) FROM salarytable GROUP BY


Employee_LastName HAVING AVG(SalaryAmount) > 18000;

SUM Function
The SUM function returns the sum of numbers. The syntax of the SUM() function is as follows:

SUM( DISTINCT I ALL numeric_expression)

You can specify either ALL or DISTINCT modifier in the SUM() function.

The DISTINCT modifier instructs the SUM() function to calculate the total of distinct
values, which means the duplicates are eliminated.

The ALL modifier allows the SUM() function to return the sum of all values
including duplicates. The SUM() function uses the ALL modifier
by default if you do not specify any modifier explicitly.

Simple SQL SUM function example


To get the sum of salary amount on salarytable, you use the SUM() function as follows:

SELECT SUM(SalaryAmount) FROM salarytable; -------- SUM(SalaryAmount)

400400.00

To get the sum of salary amount on a particular month in salarytable, you use the SUM() function as follows:

SELECT SUM(SalaryAmount) FROM salarytable WHERE SalaryMonth = 'March' SUM(SalaryAmount)

----------281600.00

SQL SUM with GROUP By clause example

To get the sum of salary amount by employees, you use the SUM() function in conjunction with a GROUP BY clause as
the following query:

SELECT Employee_LastName,Employee_FirstName, SUM(SalaryAmount) FROM salarytable GROUP BY


Employee_LastName;

The GROUP BY clause groups the salary amount by employees. For each group, the SUM() function calculate the sum of
salary amount.

SQL SUM function with HAVING clause example

To get the sum of salary amount by employee where the total salary amount is less than 45000, you need to use the
SUM() function with GROUP BY and HAVING clauses as shown below:
SELECT Employee_LastName, Employee_FirstName, SUM(SalaryAmount) FROM salarytable GROUP BY
Employee_LastName HAVING SUM(SalaryAmount) < 45000;

MIN function
The MIN function returns the minimum value in a set of values. The MIN function ignores the NULL values. The following
is the syntax of the MIN function:

MIN(DISTINCT or ALL expression)

The ALL modifier instructs the MIN function to find the minimum value in all values including duplicates. The MIN()
function uses the ALL modifier by default so you don't have to specify it explicitly. Unlike other aggregate functions e.g.,
SUM, COUNT, and AVG, the DISTINCT modifier is not applicable to the MIN() function.

Simple MIN function example

To find the lowest salary amount in the salary table, you use the MIN() function as follows:

SELECT MIN(SalaryAmount) FROM salarytable; MIN(SalaryAmount)

-----------15400.00

To get the lowest paid employees, you have to use a subquery that uses the MIN() function as the following query:

SELECT Employee_LastName, Employee_FirstName, SalaryAmount FROM salarytable WHERE SalaryAmount = ( SELECT


MIN(SalaryAmount) FROM (salarytable);

SQL MIN function with GROUP BY clause example

To find the lowest salary given to an employee in each month, you use the MIN() function with a GROUP BY clause:

SELECT SalaryMonth, MIN()SalaryAmount) FROM salarytable GROUP BY SalaryMonth;

The GROUP BY clause divides the salaries by salary month into groups. For each group, the MIN() function returns the
lowest salary amount.

SQL MIN function with HAVING clause example

To get the employee that has the lowest salary amount less than 17000, you use the MIN() function with the GROUP BY
and HAVING clauses as follows:

SELECT Employee_LastName,Employee_FirstName, MIN(SalaryAmount) FROM salarytable GROUP BY


Emplovee_LastName HAVING MIN(SalarvAmount) < 17000;

MAX function

The MAX() function returns the maximum value in a set. The MIN function does not take the NULL values into the
evaluation. The following illustrates how to use the MAX() function:

MAX(DISTINCT or ALL expression)


The ALL modifier instructs the MAX function to find the maximum value in all values including duplicates. The ALL
modifier is used by default so you do not have to specify it explicitly. Similar to the MIN function, the DISTINCT modifier
is not applicable to the MAX() function.

Simple MAX function usages


To find the highest salary amount, you use the MAX() function as the following query;

SELECT MAX(SalaryAmount) FROM salarytable; MAX(SalaryAmount)

------------19700.00

To get the highest paid employee, you need to use a subquery as follows:

SELECT Employee_LastName, Employee_FirstName, SalaryAmount FROM salarytableWHERE SalaryAmount = (SELECT


MAX(SalaryAmount ) FROM salarytable);

The subquery returns the highest salary amount in the table. Based on the highest salary, the

outer query selects the product data including last name, first name and salary.

SQL MAX function with GROUP BY example:


The following query selects the highest salary in each employee:

SELECT Employee_LastName,Employee_FirstName, MAX(SalaryAmount) FROM salarytable GROUP BY


Employee_LastName;

The GROUP BY clause divides the salaries by employee's lastname into groups. For each group, the MAX() function
returns the highest salary amount.

SQL MAX function with HAVING clause example


To get the employees that has the highest salary amount greater than 18000, you can combine the MAX() function with
the GROUP BY and HAVING clauses as the following query:

SELECT Employee_LastName, Employee _ FirstName, MAX(SalaryAmount) AS maxsalary FROM salarytable GROUP BY


Employee_LastName HAVING maxsalary > 18000

Notice that a column alias is used in both SELECT and HAVING clauses.

You might also like