Lab 10
Lab 10
Lab 10
An SQL group function or aggregate functions performs an operation on a group of rows and
returns a single result. You may want to retrieve group of item-prices and return total-
price. This type of scenario is where you would use a group function. The following table is
summary of some SQL group functions & query examples
SELECT count(product_id)FROM
COUNT(fieldname) Returns number of items in table or
product;
COUNT(*) queried items
SELECT count(*) FROM product;
SELECT stddev(price)FROM
STDDEV Returns standard deviation
inventory;
SELECT variance(price)FROM
VARIANCE Returns the variance
inventory;
To use a group function in a SQL query, list the function name followed by numeric column
name within parentheses. AVG averages the column, COUNT counts the number of items, MAX
returns maximum number of the column, and MIN returns minimum number of the column.
The following is query to retrieve total price, average price, maximum price, and minimum price
from the table "product" assuming the product table has the following values.
PRODUCT
Example
2 FROM emp
Result
------------- -------------
1400 5600
• MIN and MAX group functions can be used for any data type
Example
2 FROM emp;
Result
MIN(HIRED MAX(HIRED
--------- ---------
17-DEC-80 12-JAN-83
Example
Result
COUNT (*)
---------------
All group functions except COUNT (*) ignore null values in the column. In example below, the
average is calculated based only on the rows in the table where a valid value is stored in the
COMM column. The average is calculated as total commission being paid to all employees
divided by the number of employees receiving commission (4).
Example
2 FROM emp;
Result
AVG(COMM)
---------
550
SELECT sum (price)
FROM product;
This statement will return the total amount for the column price which is 870.
Group By is used to categorize the retrieved data. For example, you may want to list sales of
each product identified by product id. To do this, the following is Group By example that lists
The SQL GROUP BY statement is used along with the SQL aggregate functions like SUM to
provide means of grouping the result dataset by certain database table column(s).
The best way to explain how and when to use the SQL GROUP BY statement is by example, and
that’s what we are going to do.
Consider the following database table called EmployeeHours storing the daily hours for each
employee of a factious company:
If the manager of the company wants to get the simple sum of all hours worked by all
employees, he needs to execute the following SQL statement:
But what if the manager wants to get the sum of all hours for each of his employees?
To do that he need to modify his SQL query and use the SQL GROUP BY statement:
Employee Hours
John Smith 25
Allan Babel 24
Tina Crown 27
As you can see we have only one entry for each employee, because we are grouping by the
Employee column.
The SQL GROUP BY clause can be used with other SQL aggregate functions, for example SQL
AVG:
Employee Hours
Allan Babel 8
Tina Crown 9
In our Employee table we can group by the date column too, to find out what is the total
number of hours worked on each of the dates into the table:
Date Hours
5/6/2004 24
5/7/2004 27
5/8/2004 25
SQL HAVING
• Sometimes we want to retrieve the values of these functions for only those groups that
satisfy certain conditions
• The HAVING-clause is used for specifying a selection condition on groups (rather than on
individual tuples)
• The SQL HAVING clause enables conditions at the aggregate level. It is used instead of
the WHERE clause when Aggregate Functions are used.
• The HAVING clause is used to SELECT information based aggregate information. For
example, one may want to list sales representatives that have sales totaling over
$10,000. This is very useful for analytical reporting.
SYNTAX
SELECT <column_name1>, <column_name2> <aggregate_function>
FROM <table_name>
GROUP BY <column_name1>, <column_name2>
HAVING <having_condition>
EXAMPLE
FROM EMP
GROUP BY JOB
Lab Tasks
4. Show the department names of employees and the number of employees in the
department.
9. Show the department number that have budget more than 10,000
10. Show the department name and location that have budget more than 10,000
11. Display the number of employees department-wise and job-wise. The output look like as:
DEPTNO JOB COUNT (*)
------- --------- ---------
10 CLERK 1
10 MANAGER 1
10 PRESIDENT 1
20 CLERK 2
20 ANALYST 2
20 MANAGER 1
30 CLERK 1
30 MANAGER 1
30 SALESMAN 4
12. Display the highest and lowest salaries department wise. The output look like as:
DEPTNO LOWEST HIGHEST
1 1200 52000
2 800 6950
3 4560 19856
13. Display the department-wise total salary given to the employees of an organization.