ch08 1 PDF
ch08 1 PDF
ch08 1 PDF
Chapter 8
Advanced SQL (Part 1)
Objectives
• In this lecture, you will learn:
– How to implement, what SQL concepts you
learnt in the last lecture
– How to use the advanced SQL JOIN operator
syntax
– About the relational set operators UNION,
UNION ALL, INTERSECT, and MINUS
– About the different types of subqueries and
correlated queries
INSERT
UPDATE Data Manipulation Language (DML)
DELETE
CREATE
ALTER
DROP Data Definition Language (DDL)
RENAME
TRUNCATE
GRANT
Data Control Language (DCL)
REVOKE
COMMIT
Transaction Control
ROLLBACK
SQL SELECT Overview
The most basic part
SELECT
[DISTINCT | ALL] <column-list>
FROM <table-names>
[WHERE <condition>]
[ORDER BY <column-list>]
[GROUP BY <column-list>]
[HAVING <condition>]
• ([]- optional, | - or)
Example Tables
Student Grade
ID First Last ID Code Mark
S103 John Smith S103 DBS 72
S104 Mary Jones S103 IAI 58
S105 Jane Brown S104 PR1 68
S106 Mark Jones S104 IAI 65
S107 John Brown S106 PR2 43
S107 PR1 76
Course
S107 PR2 60
Code Title S107 IAI 35
DBS Database Systems
PR1 Programming 1
PR2 Programming 2
IAI Intro to AI
DISTINCT and ALL
SELECT
Student
First, Last, Mark ID First Last
FROM Student, Grade S103 John Smith
WHERE S104 Mary Jones
S105 Jane Grade
Brown
(Student.ID = S106 Mark ID JonesCode Mark
S107 John Brown
Grade.ID) AND S103 DBS 72
(Mark >= 40) S103 IAI 58
S104 PR1 68
S104 IAI 65
S106 PR2 43
S107 PR1 76
S107 PR2 60
S107 IAI 35
SELECT from Multiple Tables
Student.ID Grade.ID
SELECT from Multiple Tables
SELECT * FROM
A CROSS JOIN B SELECT * FROM
A NATURAL JOIN B
• is the same as
SELECT * FROM A, B •is the same as
SELECT A.col1,… A.coln,
[and all other columns
apart from B.col1,…B.coln]
FROM A, B
WHERE A.col1 = B.col1
AND A.col2 = B.col2
...AND A.coln = B.col.n
(this assumes that col1…
coln in A and B have
common names)
INNER JOIN
SELECT * FROM
A INNER JOIN B SELECT * FROM
ON <condition> A INNER JOIN B
USING(col1, col2,...)
• is the same as
•is the same as
SELECT * FROM A, B
WHERE <condition> SELECT * FROM A, B
WHERE A.col1 = B.col1
AND A.col2 = B.col2
AND ...
JOINs vs WHERE Clauses
Student.ID Grade.ID
SELECT from Multiple Tables
SELECT ... FROM Student, Grade
WHERE (Student.ID = Grade.ID) AND (Mark >= 40)
SELECT * FROM
Buyer INNER JOIN
Property ON
Price <= Budget
Buyer
Name Budget Address Price
Name Budget
Smith 100,000 15 High St 85,000
Smith 100,000 Jones 150,000 15 High St 85,000
Jones 150,000 Jones 150,000 12 Queen St 125,000
Green 80,000
Property
Address Price
15 High St 85,000
12 Queen St 125,000
87 Oak Row 175,000
INNER JOIN
SELECT * FROM
A INNER JOIN B SELECT * FROM
ON <condition> A INNER JOIN B
USING(col1, col2,...)
• is the same as
•is the same as
SELECT * FROM A, B
WHERE <condition> SELECT * FROM A, B
WHERE A.col1 = B.col1
AND A.col2 = B.col2
AND ...
JOINs vs WHERE Clauses
SELECT *
FROM Employee
Employee WHERE Department IN
Name Department Manager (‘Marketing’,
‘Sales’)
John Marketing Chris
Mary Marketing Chris
Chris Marketing Jane Name Department Manager
Peter Sales Jane
Jane Management John Marketing Chris
Mary Marketing Chris
Chris Marketing Jane
Peter Sales Jane
(NOT) IN
Employee SELECT *
Name Department Manager FROM Employee
WHERE Name NOT IN
John Marketing Chris (SELECT Manager
Mary Marketing Chris FROM Employee)
Chris Marketing Jane
Peter Sales Jane
Jane Management
(NOT) IN