0% found this document useful (0 votes)
72 views30 pages

SQL Select: Database Systems Lecture 7 Natasha Alechina

The document discusses SQL SELECT statements including: 1) Using WHERE clauses to restrict rows returned based on conditions. 2) Selecting from multiple tables using joins to combine information and requiring matching column values. 3) Types of joins including cross joins that return all combinations of rows and natural joins that return rows with common column names and values.

Uploaded by

Rana Gaballah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views30 pages

SQL Select: Database Systems Lecture 7 Natasha Alechina

The document discusses SQL SELECT statements including: 1) Using WHERE clauses to restrict rows returned based on conditions. 2) Selecting from multiple tables using joins to combine information and requiring matching column values. 3) Types of joins including cross joins that return all combinations of rows and natural joins that return rows with common column names and values.

Uploaded by

Rana Gaballah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

SQL SELECT

Database Systems Lecture 7


Natasha Alechina
In this Lecture
• SQL SELECT
• WHERE clauses
• SELECT from multiple tables
• JOINs
• For more information
• Connolly and Begg Chapter 5
• Ullman and Widom Chapter 6.1-6.3
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
Last
• Sometimes you end SELECT ALL Last
Smith
up with duplicate FROM Student
Jones
entries Brown
• Using DISTINCT Jones
Brown
removes duplicates
SELECT DISTINCT Last
• Using ALL retains
FROM Student Last
them - this is the
default Smith
Jones
Brown
WHERE Clauses
• Usually you don’t • Example conditions:
want all the rows • Mark < 40
• A WHERE clause • First = ‘John’
restricts the rows that • First <> ‘John’
are returned • First = Last
• It takes the form of a • (First = ‘John’)
condition - only those
AND
rows that satisfy the
condition are 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
Grade numbers and marks in
ID Code Mark IAI of students who have
passed (scored 40 or
S103 DBS 72
S103 IAI 58
higher) IAI
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 • If the tables have
combine information columns with the
from two or more same name
tables ambiguity results
• You can get the • You resolve this by
effect of a product referencing columns
by using with the table name
SELECT * FROM Table1, TableName.Column
Table2...
SELECT from Multiple Tables
Student
SELECT
ID First Last
First, Last, Mark
S103 John Smith
FROM Student, Grade S104 Mary Jones
S105 Jane Grade
Brown
WHERE S106 Mark ID JonesCode Mark
(Student.ID = S107 John Brown
S103 DBS 72
Grade.ID) AND S103 IAI 58
S104 PR1 68
(Mark >= 40) 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 Student, Grade,
almost always use a Course
WHERE clause to find WHERE
entries with common Student.ID = Grade.ID
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
• There are many types rows from A and B
of JOIN A NATURAL JOIN B
• CROSS JOIN • returns pairs of rows
• INNER JOIN with common values
• NATURAL JOIN for identically named
• OUTER JOIN columns and without
• OUTER JOINs are duplicating columns
linked with NULLs - A INNER JOIN B
more later
• 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
• INNER JOINs specify • Can also use
a condition which the SELECT * FROM
pairs of rows satisfy A INNER JOIN B
USING
SELECT * FROM (col1, col2,…)
A INNER JOIN B • Chooses rows where
ON <condition> the 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 ...
JOINs vs WHERE Clauses
• JOINs (so far) are • Yes, because
not needed • They often lead to
• You can have the concise queries
same effect by • NATURAL JOINs are
selecting from very common
multiple tables with
• No, because
an appropriate
WHERE clause • Support for JOINs
varies a fair bit
• So should you use
among SQL dialects
JOINs or not?
Writing Queries
• When writing queries • Most DBMSs have
• There are often many query optimisers
ways to write the • These take a user’s
query query and figure out
• You should worry how to efficiently
about being correct, execute it
clear, and concise in • A simple query is
that order easier to optimise
• Don’t worry about • We’ll look at some
being clever or ways to improve
efficient efficiency later
This Lecture in Exams
Track CD
cID Num Title Time aID cID Title Price
1 1 Violent 239 1 1 Mix 9.99
1 2 Every Girl 410 1 2 Compilation 12.99
1 3 Breather 217 1
1 4 Part of Me 279 1 Artist
2 1 Star 362 1
2 2 Teaboy 417 2 aID Name
1 Stellar
2 Cloudboy
This Lecture in Exams
Find a list of all the CD titles.
(1 mark)
Find a list of the titles of tracks that are more than 300
seconds long.
(2 marks)
Find a list of the names of those artists who have a track on
the CD with the title “Compilation”.
(4 marks)
Next Lecture
• More SQL SELECT
• Aliases
• ‘Self-joins’
• Subqueries
• IN, EXISTS, ANY, ALL
• For more information
• Connolly and Begg Chapter 5
• Ullman and Widom Chapter 6

You might also like