0% found this document useful (0 votes)
12 views

LECTURE - 9 Aggregation and Grouping

Aggregation and Grouping

Uploaded by

DANIAL IRFAN
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)
12 views

LECTURE - 9 Aggregation and Grouping

Aggregation and Grouping

Uploaded by

DANIAL IRFAN
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/ 39

Relational Algebra and SQL

(Aggregation and Grouping Operation)


 Relational Algebra on aggregation and grouping operation.

 SQL on 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 .

 Main aggregate functions are:


• COUNT
• SUM
• AVG
• MIN
• MAX.

3
 ISO standard defines five aggregate functions:
 COUNT returns number of values in specified column.

 SUM returns sum of values in specified column.

 AVG returns average of values in specified column.

 MIN returns smallest value in specified column.

 MAX returns largest value 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?

2. How much the total salaries for all staff?

3. What is the average of all staff’s salary?

4. What is the minimum salary of all staff?

5. What is the maximum salary of all staff?


6
Relational Algebra SQL Output
COUNT staffNo(Staff) SELECT COUNT(staffNo) 6
FROM staff;

SUM salary(Staff) SELECT SUM(salary) 102000


FROM staff;

AVG salary(Staff) SELECT AVG(salary) 17000


FROM staff;

MIN salary(Staff) SELECT MIN(salary) 9000


FROM staff;

MAX salary(Staff) SELECT MAX(salary) 30000


FROM staff;
7
 Apart from COUNT(*), each function eliminates nulls first and operates only on
remaining non-null values.
 COUNT(*) counts all rows of a table, regardless of whether nulls or duplicate values
occur.
 Can use DISTINCT before column name to eliminate duplicates.

 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)

 The general syntax for RENAME operator is :

S (B1, B2, B3,…Bn)( R)


  is the RENAME operation

 S is the new relation name

 B1, B2, B3,…Bn are the new renamed attributes (columns)

 R is the relation or table from which the attributes are chosen

10
Relational Algebra SQL
R(myCount) COUNT staffNo(Staff) SELECT COUNT(staffNo) AS myCount
FROM staff;

R(mySum) SUM salary(Staff) SELECT SUM(salary) AS mySum


FROM staff;

R(myAverage) AVG salary(Staff) SELECT AVG(salary) AS myAverage


FROM staff;

R(myMin) MIN salary(Staff) SELECT MIN(salary) AS myMin


FROM staff;

R(myMax) MAX salary(Staff) SELECT MAX(salary) AS myMax


FROM staff;
11
 Find the average of staff salary
• RA :
R (myAverage) AVG salary (Staff)

• SQL :

SELECT AVG(salary) AS myAverage


FROM staff;

• 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 :

R(myCount) COUNT * (σrent > 350 (Propertyforrent))

• SQL:

SELECT COUNT(*) AS myCount


FROM propertyforrent
WHERE rent > 350;

• 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

SELECT COUNT(DISTINCT propertyNo) AS myCount


FROM Viewing
WHERE viewDate BETWEEN ‘1-May-95’ AND ‘31-May-95’;

• 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

SELECT COUNT(staffNo) AS myCount, SUM(salary) AS mySum


FROM Staff
WHERE position = ‘Manager’;

• 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
 GAAL(R)

 Groups tuples of R by grouping attributes, GA, and then applies aggregate


function list, AL, to define a new relation.
 AL contains one or more (<aggregate_function>, <attribute>) pairs.
 Resulting relation contains the grouping attributes, GA, along with results
of each of the aggregate functions.

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.

 If SELECT list includes an aggregate function and there is no GROUP BY


clause, SELECT list cannot reference a column out with an aggregate
function.
 For example, the following SQL is illegal:

SELECT staffNo, COUNT(salary)


FROM Staff;

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);

 The right SQL statement is :

SELECT fname || ' ' || lname As Name


FROM staff
WHERE salary=(SELECT MAX(salary) FROM staff);

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

You might also like