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

SQL 3 - Group by Clause

Uploaded by

khaniqra2301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views30 pages

SQL 3 - Group by Clause

Uploaded by

khaniqra2301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

SQL 3 – GROUP BY and

Aggregate Queries
Information Systems and Databases

1
SQL 3
Objectives
• To understand how data can be grouped for aggregated
queries
• To use aggregate functions in retrieving data
• To filter aggregated data using the HAVING clause

2
SQL 3
Recap – SELECT statement

SELECT columns to display


FROM tablename
WHERE condition
GROUP BY columnname
HAVING group condition
ORDER BY

SQL 3 3
Aggregate/Group Functions

• Aggregate functions (multiple row functions)


return one result per group of rows processed.
Used to count/summarise data.
• Egs:
– AVG - gives average value of a numeric column
– SUM - gives sum of numeric column
– MAX - gives maximum value of a column
– MIN - gives minimum value of a column
– COUNT - counts the number of selected rows

SQL 3 4
AVG (column)
• Calculates the average value of a numeric
column.
Eg: To work out the average commission
(comm) for all employees in the ‘emp’ table:

SELECT AVG(comm) SELECT AVG(NVL(comm,0))


FROM emp; FROM emp;

Only averages the non- NVL function converts


nulls – so 2800/4 rows nulls to
SQLzero,
3 so 2800/14 5
rows
AVG(column)
• To work out the average value
of orders in the ‘ord’ table:

SELECT AVG(total)
FROM ord;

SQL 3 6
SUM(column)
• Calculates the total of a numeric
column.
Eg: To work out the total salary for all
employees in the ‘emp’ table:
SELECT SUM(sal)
FROM emp;

SQL 3 7
MIN(column), MAX(column)

To find the highest To find the lowest


emp salary: emp salary:

SELECT MAX(sal) SELECT MIN(sal)


FROM emp; FROM emp;

SQL 3 8
MIN(column), MAX(column)
Can also be used with non-numeric
columns:
SELECT MAX(ename), MIN(ename)
FROM emp;

SELECT MAX(hiredate), MIN(hiredate)


FROM emp;

SQL 3 9
COUNT(column) , COUNT(*)

• COUNT(column) counts the number of records having


non-null values in that column
eg COUNT(job) returns the number of rows with a
non-null value for ‘job’
• DISTINCTqualifier used to exclude duplicate
values:
COUNT(DISTINCT job) returns the number of
distinct, non-null jobs
• COUNT(*) returns the number of rows

SQL 3 10
COUNT (col) and COUNT(*)
Empno ename mgr ......
7369 SMITH 7902
7499 ALLEN 7698 Doesn’t count
7521 WARD 7698 the null
7839 KING
7878 ADAMS 7788
7788 SCOTT 7698
SELECT COUNT(*)
FROM EMP;
Gives: COUNT(*)
-------------
6
SELECT COUNT(mgr)
FROM EMP;
Gives: COUNT(mgr)
-------------
5 3
SQL 11
USING ‘GROUP BY’ CLAUSE

• Always associated with aggregate functions - rows


are grouped together by common column values and
ONE line of output produced for each distinct value in
the grouped column.
• Eg from the ‘emp’ table:
...GROUP BY deptno - gives 3 lines of output:

10 ......
20 ......
30 ......

SQL 3 12
GROUP BY

GROUP BY job gives 5 lines of output:

CLERK ......
SALESMAN ......
MANAGER ......
ANALYST ......
PRESIDENT ......
GROUP BY mgr gives 7 lines of output:
7902 ......
7698 ....
7839 ......
7566 ......
7788 ......
7566 ......
7782 ......
SQL 3 13
Eg: List the maximum salary for each department:

SELECT deptno, MAX(sal)


FROM EMP
GROUP BY deptno

...GROUP BY deptno gives 3 lines of output, one for


each distinct value of deptno:
DEPT MAX(sal)
-------- ------------
10 1300
20 800
30 1600

SQL 3 14
‘GOLDEN RULE’

• The golden rule of ‘GROUP BY’ clause is that any


column name in the SELECT clause must appear in
the GROUP BY clause, unless it’s an aggregate
function.
• So, the following is OK:
SELECT deptno, COUNT(empno)
FROM emp
GROUP BY deptno;

SQL 3 15
‘GOLDEN RULE’

• The following WOULD NOT be OK:


SELECT deptno, ename, COUNT(*)
FROM emp
GROUP BY deptno;
• The following WOULD be OK:
SELECT deptno, ename, COUNT(*)
FROM emp
GROUP BY deptno, ename;
BUT - would this produce sensible output?
SQL 3 16
SELECT deptno, ename, COUNT(*)
FROM emp
GROUP BY deptno, ename;

This would produce


‘groups’ of 1 !

SQL 3 17
GROUP BY- EXAMPLES

• To retrieve the minimum and max salary for each


job type:
SELECT job, MIN(sal), MAX(sal)
FROM emp
GROUP BY job;
• If the GROUP BY was omitted from the above, error
message:
SELECT job, MIN(sal), MAX(sal), AVG(sal)
*
ERROR at line 1: ORA-0937: not a single group set function

SQL 3 18
USING EXPRESSIONS IN ‘GROUP BY’

• In addition to column names, any expression listed in


a SELECT clause may also be used with a GROUP BY
clause
• eg, to list the average salary for each dept and also
the average salary with a payrise of 10%:
SELECT deptno, AVG(sal), AVG(sal * 1.1)
FROM emp
GROUP BY deptno;

SQL 3 19
USING ‘WHERE’ CLAUSE

• WHERE clause can be used to filter rows


prior to grouping:
SELECT mgr, COUNT(*)
FROM emp
WHERE sal > 2000
GROUP BY mgr;
Will produce a count of the number of
employees earning over 2000, managed by
each manager.
SQL 3 20
THE HAVING CLAUSE

• Like the WHERE clause, but filters out any


unwanted rows generated by the GROUP BY
clause (only used with grouped data)
• eg to list the total salary bill for each dept,but
only for depts with more than 4 employees:
SELECT deptno, SUM(sal)
FROM emp
GROUP BY deptno
HAVING COUNT(*) > 4;
SQL 3 21
To list the total salary bill for each dept,but only for depts with
more than 4 employees: Without the HAVING clause:
SELECT deptno, COUNT(*), SUM(sal)
FROM emp
GROUP BY deptno;

Adding the HAVING clause:

SELECT deptno, COUNT(*), SUM(sal)


FROM emp
GROUP BY deptno
HAVING COUNT(*) > 4;

Has eliminated deptno 10


which only has 3 employees
SQL 3 22
HAVING Clause
• Eg to select the total salary for each dept,
but only for those depts with a total salary
of over £165,000:

SELECT deptno, SUM(sal)


FROM emp
GROUP BY deptno
HAVING SUM(sal) > 165000;

SQL 3 23
To select the total salary for each dept, but only for those depts
with a total salary of over £165,000: without the HAVING clause:

SELECT deptno, SUM(sal)


FROM emp
GROUP BY deptno

Adding the HAVING clause:

SELECT deptno, SUM(sal)


FROM emp
GROUP BY deptno
HAVING SUM(sal) > 165000

SQL 3 24
‘WHERE’ and ‘HAVING’
• Eg: to list the maximum salary of each
department, excluding department 20 or any
departments with an average salary of 30000 or
more:
SELECT deptno, MAX(sal)
FROM emp
WHERE deptno != 20
GROUP BY deptno
HAVING AVG(sal) < 30000
Note - Avg(sal) does not have
to appear in the ‘select ‘ line.
SQL 3 25
Adding the ORDER BY Clause
• The ORDER BY clause comes after any HAVING
clause and must use either a group by function or a
column in the GROUP BY clause, eg
SELECT deptno, COUNT(*), AVG(sal)
FROM emp
GROUP BY deptno
HAVING COUNT(empno)> 1
ORDER BY COUNT(*);
(ORDER BY clause could also be written as ORDER BY 2)

SQL 3 26
TRY THE FOLLOWING (Using the EMP table)

1. List the total number of employees in each department.


2. List the average salary for each type of job.
3. List the total number of employees managed by each
manager, but show only managers with over 3 employees.
4. List the total number of employees hired in each month (ie
total number hired in January, total number hired in
February, etc etc)
SQL 3 27
TRY THE FOLLOWING (Using the CUSTOMER table)

1. List the total number of customers allocated to each sales


representative (repid).
2. List the average creditlimit for each sales area, excluding the
‘NW’ area.
3. List the total number of customers living in Bristol and the
total number living in Leeds.
4. List the total credit limit forSQL
each
3
city, but exclude any cities28
with a total that is less than 1000.
Summary

• The GROUP BY clause is used to divide data


into groups
• A number of aggregate functions are
available
• The HAVING clause is used to restrict
groups based on aggregated results.

SQL 3 29
Further Reading
Casteel, J (2016) – Oracle 12c SQL
Ch. 11 ‘Group Functions’

SQL 3 30

You might also like