TOPICMIDTERM
TOPICMIDTERM
Notice that all aggregate functions above ignore NULL values except for the COUNT function.
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.
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.
Simple SQL exampleTo get the number of records in the salary table, you use the COUNT(*) function as follows:
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
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.
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:
23
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.
-------17408.695652
To calculate the average of distinct salary amount, you can use the DISTINCT modifier in the AVG() function as the
following query:
-----------------17483.333333
SUM Function
The SUM function returns the sum of numbers. The syntax of the SUM() function is as follows:
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.
400400.00
To get the sum of salary amount on a particular month in salarytable, you use the SUM() function as follows:
----------281600.00
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:
The GROUP BY clause groups the salary amount by employees. For each group, the SUM() function calculate the sum of
salary amount.
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:
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.
To find the lowest salary amount in the salary table, you use the MIN() function as follows:
-----------15400.00
To get the lowest paid employees, you have to use a subquery that uses the MIN() function as the following query:
To find the lowest salary given to an employee in each month, you use the MIN() function with a GROUP BY clause:
The GROUP BY clause divides the salaries by salary month into groups. For each group, the MIN() function returns the
lowest salary amount.
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:
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:
------------19700.00
To get the highest paid employee, you need to use a subquery as follows:
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.
The GROUP BY clause divides the salaries by employee's lastname into groups. For each group, the MAX() function
returns the highest salary amount.
Notice that a column alias is used in both SELECT and HAVING clauses.