Top Banner
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
50

1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

Dec 13, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

1

CSE 480: Database Systems

Lecture 12: SQL (Nested queries and Aggregate functions)

Page 2: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

2

NESTED QUERIES

A nested query is specified within the WHERE-clause of the outer query

Page 3: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

3

NESTED QUERIES

Query: Retrieve the name and address of employees who work for the 'Research' department

SELECT Fname, Lname, AddressFROM Employee, DepartmentWHERE Dname='Research' AND Dnumber=Dno;

(Non-nested query approach)

Page 4: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

4

NESTED QUERIES

Query: Retrieve the name and address of employees who work for the 'Research' department

SELECT FNAME, LNAME, ADDRESSFROM EMPLOYEEWHERE DNO IN (SELECT DNUMBER

FROM DEPARTMENT WHERE DNAME='Research'

);

(Nested query approach)

Page 5: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

5

Example

Query: List the names of projects that involve an employee whose last name is 'Smith' as a worker or as a manager of the department that controls the project

Page 6: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

6

NESTED QUERIES

Query: List the names of projects that involve an employee whose last name is 'Smith' as a worker or as a manager of the department that controls the project

(SELECT PNAME

FROM PROJECT, DEPARTMENT, EMPLOYEEWHERE DNUM=DNUMBER AND MGRSSN=SSN

AND LNAME='Smith')UNION

(SELECT PNAMEFROM PROJECT, WORKS_ON, EMPLOYEEWHERE PNUMBER=PNO AND ESSN=SSN

AND NAME='Smith');

Non-nested query approach

Page 7: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

7

NESTED QUERIES

Query: List the names of projects that involve an employee whose last name is 'Smith' as a worker or as a manager of the department that controls the project

SELECT PNAME

FROM PROJECTWHERE PNUMBER IN

(SELECT PNUMBER FROM PROJECT,DEPARTMENT,EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN

AND LNAME='Smith') ORPNUMBER IN(SELECT PNO FROM WORKS_ON, EMPLOYEE WHERE ESSN=SSN AND NAME='Smith')

Nested query approach

Page 8: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

8

ANY, ALL, SOME

v = ANY W (e.g., Id =ANY (1,2,3))– evaluates to TRUE if v equals to one of the elements in W

v = SOME W (e.g., Id =SOME (1,2,3)) – evaluates to TRUE if v equals to some elements in W

v > ALL W (e.g., Age >ALL (23,19,34))– evaluates to TRUE if v is greater than all the elements in W

v < ALL W (e.g., Age <ALL (23,19,34))– evaluates to TRUE if v is less than all the elements in W

Page 9: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

9

NESTED QUERIES

Query: List the names of employees whose salary is greater than the salary of all managers

SELECT FNAME, LNAMEFROM EMPLOYEEWHERE SALARY > ALL (SELECT SALARY

FROM DEPARTMENT,EMPLOYEE

WHERE MGRSSN=SSN);

Page 10: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

10

THE EXISTS FUNCTION

EXISTS is used to check whether the result of a nested query is empty (contains no tuples)

SELECT

FROM

WHERE EXISTS V

– WHERE-clause evaluates to TRUE if V is not an empty table and FALSE otherwise

Page 11: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

11

THE EXISTS FUNCTION

This query will return the names of students with

GPA > 3.0

Page 12: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

12

THE EXISTS FUNCTION

Query: Retrieve the name of each employee who has a dependent with the same name as the employee’s first name.

SELEC

SELECT FNAME, LNAMEFROM EMPLOYEEWHERE EXISTS (SELECT *

FROM DEPENDENT WHERE SSN=ESSN AND

FNAME=DEPENDENT_NAME);

Correlated nested query

Page 13: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

13

THE EXISTS FUNCTION

Query: Retrieve the names of employees who have no dependents

SELECT FNAME, LNAME

FROM EMPLOYEE

WHERE NOT EXISTS (SELECT *FROM

DEPENDENTWHERE

SSN=ESSN);Correlated nested query because it involves attribute of outer query

Page 14: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

14

Division Query

Student (Id, Name)Course (CrsCode, Dept)Transcript (StudId, CrsCode, Grade)

Query: Find the names of students who have taken every course offered by the CS department

Student

1234

2345

3456

4567

5678

Course

CS

CS

CS101

CS234

Transcript

CS101

CS234

CS234

1234

1234

2345

4.0

3.5

2.5

Adam

Bill

Cathy

Didi

Eva

AdamResult

Page 15: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

15

Division Query in SQL

Student (Id, Name)Course (CrsCode, CrsName, Dept)Transcript (StudId, CrsCode, Semester, Grade)

Query: Find the names of students who have taken every course offered by the CS department

SELECT NameFROM Student WHERE ? Student has taken ALL

the CS courses

Page 16: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

16

Division Query in SQL

Student (Id, Name)Course (CrsCode, CrsName, Dept)Transcript (StudId, CrsCode, Semester, Grade)

Query: Find the names of students who have taken every course offered by the CS department

This will return names of students who have taken at least 1 CS course

SELECT NameFROM StudentWHERE EXISTS (SELECT *

FROM Transcript, CourseWHERE Transcript.StudId = Student.Id

AND Transcript.CrsCode = Course.CrsCodeAND Course.Dept = ‘CS’)

Page 17: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

17

Division Query in SQL

Student (Id, Name)Course (CrsCode, CrsName, Dept)Transcript (StudId, CrsCode, Semester, Grade)

Query: Find the names of students who have taken every course offered by the CS department

SELECT NameFROM Student WHERE ? Student has taken ALL

the CS courses

Page 18: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

18

Division Query in SQL

Student (Id, Name)Course (CrsCode, CrsName, Dept)Transcript (StudId, CrsCode, Semester, Grade)

Query: Find the names of students who have taken every course offered by the CS department

SELECT NameFROM Student WHERE (

?

)

Equivalent to saying “there are no CS courses the student has not taken”

Page 19: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

19

Division Query in SQL

Student (Id, Name)Course (CrsCode, CrsName, Dept)Transcript (StudId, CrsCode, Semester, Grade)

Query: Find the names of students who have taken every course offered by the CS department

SELECT NameFROM Student WHERE NOT EXISTS (

?)

A CS course the student has not taken

Page 20: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

20

Division Query in SQL

Student (Id, Name)Course (CrsCode, CrsName, Dept)Transcript (StudId, CrsCode, Semester, Grade)

Query: Find the names of students who have taken every course offered by the CS department

SELECT NameFROM Student WHERE NOT EXISTS (

SELECT * FROM CourseWHERE Dept = ‘CS’

AND ?)

Student has not taken the CS course

(Student, course) combination does not exist in Transcript

Page 21: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

21

Division Query in SQL

Student (Id, Name)Course (CrsCode, CrsName, Dept)Transcript (StudId, CrsCode, Semester, Grade)

Query: Find the names of students who have taken every course offered by the CS department

SELECT NameFROM Student WHERE NOT EXISTS (

SELECT * FROM CourseWHERE Dept = ‘CS’

AND NOT EXISTS (SELECT * FROM TranscriptWHERE Transcript.StudId = Student.Id AND Transcript.Crscode =

Course.Crscode) )

Page 22: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

22

Another Possible Solution

Student (Id, Name)Course (CrsCode, CrsName, Dept)Transcript (StudId, CrsCode, Semester, Grade)

Query: Find the names of students who have taken every course offered by the CS department

SELECT NameFROM Student WHERE NOT EXISTS (

SELECT * FROM CourseWHERE Dept = ‘CS’

AND CrsCode NOT IN (SELECT CrsCode FROM TranscriptWHERE Transcript.StudId = Student.Id)

)

Page 23: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

23

Division Query

Retrieve the names of employees who work on every project controlled by the “Research” department

Page 24: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

24

Example: Division Query

Retrieve the names of employees who work on every project controlled by the “Research” department

SELECT DISTINCT E.Lname, E.Fname

FROM EMPLOYEE E

WHERE NOT EXISTS

(

A project controlled by the Research

department in which the employee had

NOT worked on

)

Page 25: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

25

Example: Division Query

Retrieve the names of employees who work on every project controlled by the “Research” department

SELECT DISTINCT E.Lname, E.Fname

FROM EMPLOYEE E

WHERE NOT EXISTS

( SELECT P.Pnumber

FROM DEPARTMENT D, PROJECT P

WHERE D.Dnumber = P.Dnum AND D.Dname=‘Research’

AND NOT EXISTS (

(employee,project) combination

in the Works_On table

)

)

Page 26: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

26

Example: Division Query

Retrieve the names of employees who work on every project controlled by the “Research” department

SELECT DISTINCT E.Lname, E.Fname

FROM EMPLOYEE E

WHERE NOT EXISTS

(SELECT P.Pnumber

FROM DEPARTMENT D, PROJECT P

WHERE D.Dnumber = P.Dnum AND D.Dname=‘Research’

AND NOT EXISTS (

SELECT *

FROM WORKS_ON W

WHERE W.ESSN=E.SSN AND P.Pnumber=W.Pno))

Page 27: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

27

Exercise

Find the names of projects worked on by every employee in the Research department

– Exclude the projects worked on only by a few but not all employees in the Research department

Find the names of employees who work for the Research department but do not work on all the projects controlled by the Research department

Page 28: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

28

Aggregate Functions

Used to compute summary statistics for a group of tuples in a table Include COUNT, SUM, MAX, MIN, and AVG

Page 29: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

29

Aggregate Functions

Need to be careful when dealing with NULL values

Page 30: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

30

Aggregate Functions

Query: Find the sum of the salaries, maximum salary, minimum salary, and average salary of employees in the ‘Research’ department

Page 31: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

31

Aggregate Functions

Query: Count the number of employees who work for the 'Research' department

Page 32: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

32

Aggregate Functions

Query: Count the number of employees in department number 3 who have worked on at least one project

overestimate

Page 33: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

33

Aggregate Functions

Query: Count the number of employees in Department number 3 who have worked on at least one project

Page 34: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

34

Aggregate Functions

Cannot use aggregate function directly in the WHERE clause

Query: Find the id of employees who have two or more dependents

Page 35: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

35

Aggregate Functions

Query: Find the name of employees who have two or more dependents

This is ok

Page 36: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

36

Grouping Clause

Suppose we want to apply the aggregate functions to subgroups of tuples in a relation– Each subgroup consists of the set of tuples that have the same

value for the grouping attribute(s)

SQL has a GROUP BY-clause for specifying the grouping attributes

Page 37: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

37

Grouping Clause Example

Query: For each department, retrieve the department number, the number of employees in the department, and their average salary.

SELECT DNO, COUNT (*),

AVG (SALARY)FROM EMPLOYEEGROUP BY DNO;

– The EMPLOYEE tuples are divided into groups, each group having the same value for the grouping attribute DNO The COUNT and AVG functions are applied to each such group of

tuples separately The SELECT-clause includes only the grouping attribute and the

aggregate functions to be applied on each group of tuples

Page 38: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

38

Grouping Clause Example

Effect of null value

Page 39: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

39

Grouping Clause Example

Query: For each project, retrieve the project number, project name, and the number of employees who work on that project.

In this case, the grouping and aggregate functions are applied after joining the two relations

Page 40: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

40

Grouping Clause Example

Query: For each project, retrieve the project number, project name, and number of employees from department 3 who work on the

project

Page 41: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

41

HAVING Clause

The HAVING-clause is used to retrieve only groups whose values of the aggregate functions satisfy certain condition

Page 42: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

42

HAVING Clause Example

Query: For each project that has more than one employees work, retrieve the project number, project name, and the number of employees who work on that project.

Page 43: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

43

HAVING Clause Example

Query: For each project that has more than one employees work on it, retrieve the project number, project name, and the number of employees who work on that project.

Page 44: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

44

HAVING Clause Example

Query: For each department that has more than 1 employee, retrieve the department name and the number of employees who are making more than $50,000

Answer should be: (Payroll, 1) and (Research, 1)

Page 45: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

45

HAVING Clause

WRONG! This query looks for departments that have more than 1 employee making more than $50,000

Query: For each department that has more than 1 employee, retrieve the department name and the number of employees who are making more than $50,000

Page 46: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

46

HAVING Clause

Query: For each department that has more than 1 employee, retrieve the department name and the number of employees who are making more than $50,000

Page 47: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

47

ORDER BY Clause

Sort tuples based on the values of some attribute(s)

Query: Retrieve a list of employees and the projects they work on, ordered by their department name, and within each department, ordered alphabetically by last name and first name.

SELECT Dname, Lname, Fname, Pname

FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHERE Dnumber=Dno AND SSN=ESSN

AND Pno=Pnumber ORDER BY Dname, Lname, Fname;

The default order is in ascending order of values– We can also specify the keyword DESC or ASC

Page 48: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

48

ORDER BY Clause

Query: Retrieve a list of employees and the projects they work on, ordered by their department name, and within each department, ordered alphabetically by salary (in descending order)

SELECT Dname, Lname, Fname,

Pname FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT

WHERE Dnumber=Dno AND SSN=ESSN AND Pno=Pnumber

ORDER BY Dname ASC, Salary DESC;

Page 49: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

49

Summary of SQL Queries

SELECT <attribute list>FROM <table list>[WHERE <condition>][GROUP BY <grouping attribute(s)>][HAVING <group condition>][ORDER BY <attribute list>]

SELECT-clause lists the attributes/functions to be retrieved FROM-clause specifies all relations (or aliases) needed in the query

but not those needed in nested queries WHERE-clause specifies the conditions for selection and join of

tuples from the relations specified in the FROM-clause GROUP BY specifies grouping attributes HAVING specifies a condition for selection of groups ORDER BY specifies an order for displaying the result of a query

A query is evaluated by first applying the WHERE-clause, then GROUP BY and HAVING, and finally the SELECT-clause

Page 50: 1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)

50

Limit (in MySQL)

Find the name and salary of the highest paid employee