Lecture 4 - Groupings Flashcards
The process of grouping is?
Partition a relation into groups based on grouping attribute, i.e.,
clustering tuples having the same value in the grouping attribute.
What is the statement that can be used for grouping?
GROUP BY
Example of grouping by departement and working out the average salary?
SELECT DNO, COUNT (*), AVG (Salary)
FROM EMPLOYEE
GROUP BY DNO;
The grouping attribute must appear…
in SELECT
What happens if a grouping attribute has NULL values?
There is a seperate group created for NULLS.
Is grouping computationally heavy?
yes
What should we avoid doing?
Grouping by PK as this doesn’t do anything
EXAMPLE : How many employees are working in each project? Include the
project name at the results
SELECT P.PNAME, COUNT (*) //employees per project
FROM PROJECT AS P, WORKS_ON AS W
WHERE P.PNUMBER = W.PNO
GROUP BY P.PNAME;
Is it possible to group by more than 1 attribute ?
YEs
Example of multi-attribute grouping?
SELECT COUNT(*), E.DNO, E.SALARY
FROM EMPLOYEE AS E
GROUP BY E.DNO, E.SALARY
ORDER BY E.DNO