LECTURE - 9 Aggregation and Grouping
LECTURE - 9 Aggregation and Grouping
2
AL(R)
Applies aggregate function list, AL, to R to define a relation over the aggregate
list.
AL contains one or more (<aggregate_function>, <attribute>) pairs .
3
ISO standard defines five aggregate functions:
COUNT returns number of values in specified column.
4
Each operates on a single column of a table and returns a single value.
COUNT, MIN, and MAX apply to numeric and non-numeric fields, but
SUM and AVG may be used on numeric fields only.
5
1. How many staff works for Dreamhome?
DISTINCT has no effect with MIN/MAX, but may have with SUM/AVG.
8
Viewing Table
SELECT COUNT(*)
FROM viewing;
SELECT COUNT(Comments)
FROM viewing;
SELECT COUNT(ClientNo)
FROM viewing;
SELECT COUNT(Distinct ClientNo)
9
FROM viewing;
The RENAME operator is symbolized by (rho)
10
Relational Algebra SQL
R(myCount) COUNT staffNo(Staff) SELECT COUNT(staffNo) AS myCount
FROM staff;
• SQL :
• Output :
12
Find the number of staff and the sum of their salaries
• RA :
R(myCount, mySum) COUNT staffNo, SUM salary (Staff)
• SQL :
SELECT COUNT(staffno) AS MYCOUNT,
SUM(salary)AS MYSUM
FROM staff;
• Output :
13
How many staff earn more than 10,000?
14
How many staff earn more than 10,000?
RA :
R(myCount) COUNT staffNo (σsalary > 10000 (Staff))
SQL :
SELECT COUNT(staffno) AS myCount
FROM staff
WHERE salary>10000;
Output :
15
Pearson Education © 2009
How many properties cost more than 350 per month to rent?
propertyforrent Table
16
How many properties cost more than 350 per month to rent?
• RA :
• SQL:
• Output:
17
How many different properties viewed in May 95?
Viewing Table
18
)
How many different properties viewed in May 95?
• RA
R(myCount) COUNT propertyno (σviewdate between ‘1-May-95’ ^ ‘31-May-95’ (Propertyforrent))
• SQL
• Output:
19
Find the number of Managers and sum of their salaries.
20
Find the number of Managers and sum of their salaries.
• RA
R(myCount, mySUM) COUNT staffNo, SUM salary (σposition=’Manager’ (Staff))
• SQL
• Output
21
Find minimum, maximum, and average staff salary.
• RA
R(myCount, mySUM, myAvg) MIN salary, MAX salary, AVG salary (Staff)
• SQL
SELECT MIN(salary) AS myMin,
MAX(salary) AS myMax,
AVG(salary) AS myAvg
FROM Staff;
• Output :
22
GAAL(R)
23
SQL Syntax:
SELECT columnlist
FROM tablelist
[WHERE conditionlist]
[GROUP BY columnlist]
[HAVING conditionlist]
[ORDER BY columnlist [ASC | DESC] ] ;
24
25
Find the number of staff working in each branch and the sum of their salaries.
RA :
R(branchNo, myCount, mySum) branchNo COUNT staffNo, SUM salary (Staff)
SQL :
SELECT branchno, COUNT(staffno) AS myCount,
SUM(salary) As mySUM
FROM staff
GROUP BY branchno;
Output:
26
Aggregate functions can be used only in SELECT list and in HAVING
clause.
27
Give the name of staff who get the highest salary?
This SQL statement is illegal :
SELECT fname || ' ' || lname As Name
FROM staff
WHERE salary = MAX(salary);
28
Use GROUP BY clause to get sub-totals.
SELECT and GROUP BY are closely integrated: each item in SELECT list
must be single-valued per group, and SELECT clause may only contain:
column names
aggregate functions
constants
expression involving combinations of the above.
29
All column names in SELECT list must appear in GROUP BY
clause unless name is used only in an aggregate function.
If WHERE is used with GROUP BY, WHERE is applied first, then
groups are formed from remaining rows satisfying predicate.
ISO considers two nulls to be equal for purposes of GROUP BY.
30
Find number of staff in each branch and their total salaries.
31
Find the number of staff in each branch and their total salaries.
• RA :
R(branchNo, myCount, mySum) branchNo COUNT staffNo, SUM salary (Staff)
• SQL :
SELECT branchNo,COUNT(staffNo) AS myCount, SUM(salary) AS mySum
FROM Staff
GROUP BY branchNo
ORDER BY branchNo;
32
• Output :
33
HAVING clause is designed for use with GROUP BY to restrict
groups that appear in final result table.
Similar to WHERE, but WHERE filters individual rows whereas
HAVING filters groups.
Column names in HAVING clause must also appear in the GROUP
BY list or be contained within an aggregate function.
34
For each branch with more than 1 member of staff, display the number of staff
in each branch and sum of their salaries.
• RA :
R(branchNo, myCount, mySum) (σCOUNT(staffNo) > 1 ( branchNo COUNT staffNo, SUM salary (Staff)))
• SQL :
SELECT branchNo, COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM staff
GROUP BY branchNo
HAVING COUNT(staffNo) > 1
ORDER BY branchNo;
35
• Output :
36
For each branch with more than 1 member of staff, find the number of staff who
earn more than 10000 in each branch and the sum of their salaries.
• RA :
R(branchNo, myCount, mySum) (σCOUNT(staffNo) > 1 ( branchNo COUNT staffNo, SUM
salary (σsalary > 10000(Staff))))
• SQL :
SELECT branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM staff
WHERE salary>10000
GROUP BY branchNo
HAVING COUNT(staffNo) > 1
ORDER BY branchNo;
37
Output:
38
39