SQL Advanced
SQL Advanced
Database System I
Lecture 4. SQL Advanced
Sept 20 /18
1
Announcements!
• A1 is due today
2
Outline
• Joins
• Inner Join
• Outer Join
• Aggregation Queries
• Simple Aggregations
• Group By
• Having
• Discussion
3
Joins: Recap
Student Enroll
name gpa stdName course
Mary 3.8 Mary 354
Tom 3.6 Tom 354
Jack 3.7 Tom 454
Bob 354
name gpa
Mary 354
Tom 354
Tom 454 4
Two equivalent ways to write
joins
SELECT name, course
FROM Student, Enroll
WHERE name = stdName
5
Join Types
SELECT name, course
FROM Student INNER JOIN Enroll ON
name = stdName
SELECT name
FROM Student LEFT OUTER JOIN Enroll ON
name = stdName
SELECT name
FROM Student RIGHT OUTER JOIN Enroll ON
name = stdName 6
Join Types
SELECT name, course
FROM Student INNER JOIN Enroll ON
name = stdName
SELECT name
FROM Student LEFT OUTER JOIN Enroll ON
name = stdName
SELECT name
FROM Student RIGHT OUTER JOIN Enroll ON
name = stdName 7
Left Join
Student Enroll
name gpa stdName course
Mary 3.8 Mary 354
Tom 3.6 Tom 354
Jack 3.7 Tom 454
Bob 354
Output
name course
Mary 354
Tom 354
Tom 454
Jack NULL
9
SELECT name, course
FROM Student RIGHT JOIN Enroll ON
name = stdName
Student Enroll
name gpa stdName course
Mary 3.8 Mary 354
Tom 3.6 Tom 354
Jack 3.7 Tom 454
Bob 354
Output
name course
Mary 354
Tom 354
Tom 454
NULL 354
10
SELECT name, course
FROM Enroll FULL JOIN Student ON
name = stdName
Enroll Student
stdName course name gpa
Mary 354 Mary 3.8
Tom 354 Tom 3.6
Tom 454 Jack 3.7
Bob 354
Output
name course
Mary 354
Tom 354
Tom 454
Jack NULL
NULL 354 11
Outer Join
TableA (LEFT/RIGHT/FULL) JOIN TableB
12
Exercise - 1
SELECT name, course
FROM Student LEFT JOIN Enroll ON
name = stdName AND course = 354
Student Enroll
name gpa stdName course
Mary 3.8 Mary 354
Tom 3.6 Tom 354
Jack 3.7 Tom 454
Bob 354
name course
A Mary 354 name course
Tom 354 Mary 354
Jack NULL Tom 354
13
(A) (B)
Exercise - 2
SELECT name, course
FROM Student LEFT JOIN Enroll ON
name = stdName
WHERE course = 354
Student Enroll
name gpa stdName course
Mary 3.8 Mary 354
Tom 3.6 Tom 354
Jack 3.7 Tom 454
Bob 354
15
Simple Aggregation
SELECT agg(column)
FROM <table name>
WHERE <conditions>
gender AVG(gpa)
gender AVG(gpa)
VS F 4
F 4
M NULL
(A) (B) 25
Exercise: Empty Group
name gender gpa
Bob M 3 SELECT gender, AVG(gpa)
Mike M 3 FROM Student
Alice F 4 WHERE gpa > 3.5
Mary F 4 GROUP BY gender
Tom M 3
26
Exercise: Empty Group
name gender gpa
Bob M 3 SELECT gender, AVG(gpa)
Mike M 3 FROM Student
Alice F 4 WHERE gpa > 3.5
Mary F 4
GROUP BY gender
Tom M 3
27
Exercise: Empty Group
name gender gpa
Bob M 3 SELECT gender, AVG(gpa)
Mike M 3 FROM Student
Alice F 4 WHERE gpa > 3.5
Mary F 4 GROUP BY gender
Tom M 3
28
Exercise: Invalid Selection
name gender gpa
Bob M 3 SELECT gender, AVG(gpa), name
Mike M 3 FROM Student
Alice F 4 WHERE gpa > 3.5
Mary F 4 GROUP BY gender
Tom M 3
(A) (B) 29
Exercise: Invalid Selection
name gender gpa
Bob M 3 SELECT gender, AVG(gpa), name
Mike M 3 FROM Student
Alice F 4 WHERE gpa > 3.5
Mary F 4 GROUP BY gender
Tom M 3
30
Exercise: Invalid Selection
name gender gpa
Bob M 3 SELECT gender, AVG(gpa), name
Mike M 3 FROM Student
Alice F 4 WHERE gpa > 3.5
Mary F 4 GROUP BY gender
Tom M 3
31
Exercise: Invalid Selection
Everything in SELECT must be either a
GROUP-BY attribute, or an aggregate
name gender gpa
Bob M 3 SELECT gender, AVG(gpa), name
Mike M 3 FROM Student
Alice F 4 WHERE gpa > 3.5
Mary F 4 GROUP BY gender
Tom M 3
32
HAVING Clause
• Specify which groups you are interested in
SELECT agg(column)
FROM <table name>
WHERE <conditions>
GROUP BY <columns>
HAVING <columns>
33
HAVING Clause
• Same query as before, except that we require each
group has more than 10 students
35
Exercise
StudentInfo
name gender gpa
Bob M 3
Mike M 3
Alice F 4
Mary F 4
Tom M 3
M 3 F 4 M 3
F 4
36
(A) (B) (C)
Imagine you are a data scientist
at a Bank
37
Computer Science vs. Data Science
38
Discussion
39
Discussion
40
Outline
• Joins
• Inner Join
• Outer Join
• Self Join
• Aggregation Queries
• Simple Aggregations
• Group By
• Having
• Subqueries
• In the FROM clause
• In the WHERE clause
41
Subqueries
• A subquery is a SQL query nested inside a larger query
• Such inner-outer queries are called nested queries
43
Subqueries in FROM
• Sometimes we need to compute an intermediate
table only to use it later in a SELECT-FROM-WHERE
• Who is the richest customer?
44
Subqueries in FROM
• Sometimes we need to compute an intermediate
table only to use it later in a SELECT-FROM-WHERE
• Which customers have a total balance equal to 0?
SELECT C.customerID
FROM Customer C1
WHERE C1.income > (SELECT avg(C2.income)
FROM Customer C2)
47
Subqueries in WHERE
• Subqueries return a relation
• IN
• NOT IN
• EXISTS
• NOT EXISTS
• ANY
• ALL
48
Accounts IN Burnaby
• Find the customerIDs of customers with an account at
the Burnaby branch
SELECT C.customerID
FROM Customer C
WHERE C.customerID IN (SELECT O.customerID
FROM Account A, Owns O
WHERE A.accNumber = O.accNumber
AND A.branchName = ’Burnaby’)
49
Accounts NOT IN Burnaby
• Find the customerIDs of customers who do not have an
account at the Burnaby branch
SELECT C.customerID
FROM Customer C
WHERE C.customerID NOT IN(
SELECT O.customerID
FROM Account A, Owns O
WHERE A.accNumber = O.accNumber AND
A.branchName = ’Burnaby’)
50
Uncorrelated Queries
• The query shown previously contains an uncorrelated,
or independent, sub-query
• The sub-query does not contain references to attributes of
the outer query
53
EXISTing BurnabyAccounts
• Find the customerIDs of customers with an account at
the Burnaby branch
54
Have an account in all branches
• Find the customerIDs of customers who have an
account in all branches
SQ1 – A list of all SQ2 – A list of
branch names branch names that
a customer has an
account at
EXCEPT
SELECT C.customerID
FROM Customer C
WHERE NOT EXISTS ( (SELECT B.branchName
FROM Branch B)
EXCEPT
(SELECT A.branchName
FROM Account A, Owns O
WHERE O.customerID = C.customerID
AND O.accNumber = A.accNumber))
56
ANYone Richer Than Bruce
• Find the customerIDs of customers who earn more
than some customer called Bruce
SELECT C.customerID
FROM Customer C
WHERE C.income > ANY
(SELECT Bruce.income
FROM Customer Bruce
WHERE Bruce.firstName = 'Bruce')