Lecture 12: SQL (Nested Queries and Aggregate Functions)
Lecture 12: SQL (Nested Queries and Aggregate Functions)
1
NESTED QUERIES
2
NESTED QUERIES
3
NESTED QUERIES
5
NESTED QUERIES
(SELECT PNAME
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND LNAME='Smith')
UNION
(SELECT PNAME
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND ESSN=SSN
AND NAME='Smith');
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
WHERE PNUMBER IN
(SELECT PNUMBER
FROM PROJECT,DEPARTMENT,EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND LNAME='Smith')
OR
PNUMBER IN
(SELECT PNO
FROM WORKS_ON, EMPLOYEE
WHERE ESSN=SSN AND NAME='Smith')
7
ANY, ALL, SOME
8
NESTED QUERIES
9
THE EXISTS FUNCTION
SELECT
FROM
WHERE EXISTS V
10
THE EXISTS FUNCTION
11
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
12
THE EXISTS FUNCTION
Result Adam
14
Division Query in SQL
15
Division Query in SQL
17
Division Query in SQL
18
Division Query in SQL
19
Division Query in SQL
22
Division Query
23
Example: Division Query
24
Example: Division Query
25
Example: Division Query
26
Exercise
27
Aggregate Functions
28
Aggregate Functions
29
Aggregate Functions
30
Aggregate Functions
Query: Count the number of employees who work for the 'Research'
department
31
Aggregate Functions
overestimate
32
Aggregate Functions
33
Aggregate Functions
34
Aggregate Functions
This is ok
35
Grouping Clause
36
Grouping Clause Example
37
Grouping Clause Example
38
Grouping Clause Example
Query: For each project, retrieve the project number, project name,
and the number of employees who work on that project.
39
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
40
HAVING Clause
41
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.
42
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.
43
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
44
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
WRONG! This query looks for departments that have more than
1 employee making more than $50,000
45
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
46
ORDER BY Clause
47
ORDER BY Clause
48
Summary of SQL Queries
SELECT <attribute list>
FROM <table list>
[WHERE <condition>]
[GROUP BY <grouping attribute(s)>]
[HAVING <group condition>]
[ORDER BY <attribute list>]
50