You are on page 1of 31

SQL SELECT AND JOINS

Database Management System


SQL SELECT Overview
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
• Sometimes you end up with SELECT ALL Last Last
duplicate entries Smith
FROM Student
• Using DISTINCT removes Jones
Brown
duplicates Jones
• Using ALL retains them - Brown
this is the default
SELECT DISTINCT Last
Last
FROM Student
Smith
Jones
Brown
WHERE Clauses
• Usually you don’t want all
• Example conditions:
the rows
• Mark < 40
• A WHERE clause restricts the
rows that are returned • First = ‘John’
• It takes the form of a • First <> ‘John’
condition - only those rows • First = Last
that satisfy the condition are • (First = ‘John’) AND
returned (Last = ‘Smith’)
• (Mark < 40) OR (Mark
> 70)
WHERE Examples
SELECT * FROM Grade SELECT DISTINCT ID
WHERE Mark >= 60 FROM Grade
WHERE Mark >= 60
ID Code Mark
S103 DBS 72 ID
S104 PR1 68 S103
S104 IAI 65 S104
S107 PR1 76 S107
S107 PR2 60
WHERE Example
• Given the table • Write an SQL query to find a list
of the ID numbers and marks in
IAI of students who have passed
Grade (scored 40 or higher) IAI
ID Code Mark
S103 DBS 72
S103 IAI 58
S104 PR1 68
S104 IAI 65 ID Mark
S106 PR2 43 S103 58
S107 PR1 76 S104 65
S107 PR2 60
S107 IAI 35
One Solution

We only want the ID and Mark, not the Code


Single quotes around the string

SELECT ID, Mark FROM Grade


WHERE (Code = ‘IAI’) AND
(Mark >= 40)

We’re only interested in IAI


We’re looking for entries with pass marks
SELECT from Multiple Tables
• Often you need to combine • If the tables have columns
information from two or with the same name
more tables ambiguity results
• You can get the effect of a • You resolve this by
product by using referencing columns with
SELECT * FROM Table1, the table name
Table2... TableName.Column
SELECT from Multiple Tables
SELECT Student
First, Last, Mark ID First Last

FROM Student, Grade S103 John Smith


S104 Mary Jones
WHERE S105 Jane Grade
Brown
S106 Mark ID JonesCode Mark
(Student.ID = S107 John Brown
S103 DBS 72
Grade.ID) AND S103 IAI 58
(Mark >= 40) S104 PR1 68
S104 IAI 65
S106 PR2 43
S107 PR1 76
S107 PR2 60
S107 IAI 35
SELECT from Multiple Tables
SELECT ... FROM Student, Grade WHERE...

ID First Last ID Code Mark


S103 John Smith S103 DBS 72
Are matched S103 John Smith S103 IAI 58 All of the
with the first S103 John Smith S104 PR1 68 entries from
entry from S103 John Smith S104 IAI 65 the Grade
the Student S103 John Smith S106 PR2 43 table
table... S103 John Smith S107 PR1 76
S103 John Smith S107 PR2 60
S103 John Smith S107 IAI 35
And then S104 Mary Jones S103 DBS 72
with the S104 Mary Jones S103 IAI 58
second… S104 Mary Jones S104 PR1 68
S104 Mary Jones S104 IAI 65
and so on S104 Mary Jones S106 PR2 43
SELECT from Multiple Tables
SELECT ... FROM Student, Grade
WHERE (Student.ID = Grade.ID) AND ...

ID First Last ID Code Mark


S103 John Smith S103 DBS 72
S103 John Smith S103 IAI 58
S104 Mary Jones S104 PR1 68
S104 Mary Jones S104 IAI 65
S106 Mark Jones S106 PR2 43
S107 John Brown S107 PR1 76
S107 John Brown S107 PR2 60
S107 John Brown S107 IAI 35

Student.ID Grade.ID
SELECT from Multiple Tables
SELECT ... FROM Student, Grade
WHERE (Student.ID = Grade.ID) AND (Mark >= 40)

ID First Last ID Code Mark


S103 John Smith S103 DBS 72
S103 John Smith S103 IAI 58
S104 Mary Jones S104 PR1 68
S104 Mary Jones S104 IAI 65
S106 Mark Jones S106 PR2 43
S107 John Brown S107 PR1 76
S107 John Brown S107 PR2 60
SELECT from Multiple Tables
SELECT First, Last, Mark FROM Student, Grade
WHERE (Student.ID = Grade.ID) AND (Mark >= 40)

First Last Mark


John Smith 72
John Smith 58
Mary Jones 68
Mary Jones 65
Mark Jones 43
John Brown 76
John Brown 60
SELECT from Multiple Tables
• When selecting from SELECT * FROM
multiple tables you almost Student, Grade, Course
always use a WHERE clause WHERE
to find entries with
Student.ID = Grade.ID
common values
AND
Course.Code =
Grade.Code
SELECT from Multiple Tables
Student Grade Course

ID First Last ID Code Mark Code Title


S103 John Smith S103 DBS 72 DBS Database Systems
S103 John Smith S103 IAI 58 IAI Intro to AI
S104 Mary Jones S104 PR1 68 PR1 Programming 1
S104 Mary Jones S104 IAI 65 IAI Intro to AI
S106 Mark Jones S106 PR2 43 PR2 Programming 2
S107 John Brown S107 PR1 76 PR1 Programming 1
S107 John Brown S107 PR2 60 PR2 Programming 2
S107 John Brown S107 IAI 35 IAI Intro to AI

Student.ID = Grade.ID Course.Code = Grade.Code


JOINs
• JOINs can be used to
A CROSS JOIN B
combine tables
• returns all pairs of rows from
• There are many types of A and B
JOIN
• CROSS JOIN A NATURAL JOIN B
• INNER JOIN • returns pairs of rows with
• NATURAL JOIN common values for
• OUTER JOIN identically named columns
• OUTER JOINs are linked and without duplicating
columns
with NULLs - more later
A INNER JOIN B
• returns pairs of rows
satisfying a condition
CROSS JOIN
Student SELECT * FROM
ID Name Student CROSS JOIN
123 John Enrolment
124 Mary
125 Mark ID Name ID Code
126 Jane 123 John 123 DBS
124 Mary 123 DBS
Enrolment 125 Mark 123 DBS
ID Code 126 Jane 123 DBS
123 John 124 PRG
123 DBS 124 Mary 124 PRG
124 PRG 125 Mark 124 PRG
124 DBS 126 Jane 124 PRG
126 PRG 123 John 124 DBS
124 Mary 124 DBS
NATURAL JOIN
Student SELECT * FROM
ID Name Student NATURAL JOIN
123 John
Enrolment
124 Mary
125 Mark
126 Jane ID Name Code

Enrolment 123 John DBS


124 Mary PRG
ID Code 124 Mary DBS
123 DBS 126 Jane PRG
124 PRG
124 DBS
126 PRG
CROSS and NATURAL JOIN
SELECT * FROM
SELECT * FROM
A CROSS JOIN B
A NATURAL JOIN B
• is the same as
•is the same as
SELECT * FROM A, B
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
• Can also use
• INNER JOINs specify a
condition which the pairs of SELECT * FROM
rows satisfy A INNER JOIN B
USING
SELECT * FROM (col1, col2,…)
A INNER JOIN B • Chooses rows where the
ON <condition> given columns are equal
INNER JOIN
Student SELECT * FROM
ID Name Student INNER JOIN
123 John
Enrolment USING (ID)
124 Mary
125 Mark ID Name ID Code
126 Jane 123 John 123 DBS
Enrolment 124 Mary 124 PRG
124 Mary 124 DBS
ID Code 126 Jane 126 PRG
123 DBS
124 PRG
124 DBS
126 PRG
INNER JOIN
Buyer
SELECT * FROM Name Budget
Buyer INNER JOIN Smith 100,000
Property ON Jones 150,000
Price <= Budget Green 80,000

Property Name Budget Address Price

Address Price Smith 100,000 15 High St 85,000


Jones 150,000 15 High St 85,000
15 High St 85,000 Jones 150,000 12 Queen St 125,000
12 Queen St 125,000
87 Oak Row 175,000
INNER JOIN
SELECT * FROM
SELECT * FROM
A INNER JOIN B
A INNER JOIN B
ON <condition>
USING(col1, col2,...)
• is the same as
•is the same as
SELECT * FROM A, B
SELECT * FROM A, B
WHERE <condition>
WHERE A.col1 = B.col1
AND A.col2 = B.col2
AND ...
OUTER JOINS
• When we take the join of • Outer joins include dangles
two relations we match up in the result and use NULLs
tuples which share values to fill in the blanks
• Some tuples have no match, • Left outer join
and are ‘lost’ • Right outer join
• These are called ‘dangles’ • Full outer join
Example: inner join
Student Enrolment
ID Name ID Code Mark
123 John 123 DBS 60
124 Mary 124 PRG 70
125 Mark 125 DBS 50
126 Jane 128 DBS 80 dangles

Student inner join


Enrolment
ID Name ID Code Mark
123 John 123 DBS 60
124 Mary 124 PRG 70
125 Mark 125 DBS 50
Example: full outer join
Student Enrolment
ID Name ID Code Mark
123 John 123 DBS 60
124 Mary 124 PRG 70
125 Mark 125 DBS 50
126 Jane 128 DBS 80 dangles

Student full outer join Enrolment


ID Name ID Code Mark
123 John 123 DBS 60
124 Mary 124 PRG 70
125 Mark 125 DBS 50
126 Jane null null null
null null 128 DBS 80
Example: left outer join
Student Enrolment
ID Name ID Code Mark
123 John 123 DBS 60
124 Mary 124 PRG 70
125 Mark 125 DBS 50
126 Jane 128 DBS 80 dangles

Student left outer join Enrolment


ID Name ID Code Mark
123 John 123 DBS 60
124 Mary 124 PRG 70
125 Mark 125 DBS 50
126 Jane null null null
Example: right outer join
Student Enrolment
ID Name ID Code Mark
123 John 123 DBS 60
124 Mary 124 PRG 70
125 Mark 125 DBS 50
126 Jane 128 DBS 80 dangles

Student right outer join Enrolment


ID Name ID Code Mark
123 John 123 DBS 60
124 Mary 124 PRG 70
125 Mark 125 DBS 50
null null 128 DBS 80
Outer Join Syntax in Oracle
SELECT <cols>
FROM <table1> <type> OUTER JOIN <table2>
ON <condition>

Where <type> is one of LEFT, RIGHT, or FULL

Example:
SELECT *
FROM Student FULL OUTER JOIN Enrolment
ON Student.ID = Enrolment.ID

Missing Information
JOINs vs WHERE Clauses
• JOINs (so far) are not • Yes, because
needed • They often lead to concise
• You can have the same queries
effect by selecting from • NATURAL JOINs are very
multiple tables with an common
appropriate WHERE clause
• No, because
• So should you use JOINs or
not? • Support for JOINs varies a
fair bit among SQL dialects

You might also like