Содержание

Слайд 2

In this Lecture

SQL SELECT
WHERE clauses
SELECT from multiple tables
JOINs
For more information
Connolly and Begg

In this Lecture SQL SELECT WHERE clauses SELECT from multiple tables JOINs
Chapter 5
Ullman and Widom Chapter 6.1-6.3

Слайд 3

SQL SELECT Overview

SELECT
[DISTINCT | ALL]
FROM
[WHERE ]
[ORDER BY ]
[GROUP BY

SQL SELECT Overview SELECT [DISTINCT | ALL] FROM [WHERE ] [ORDER BY
]
[HAVING ]
([]- optional, | - or)

Слайд 4

Example Tables

Example Tables

Слайд 5

DISTINCT and ALL

Sometimes you end up with duplicate entries
Using DISTINCT removes duplicates
Using

DISTINCT and ALL Sometimes you end up with duplicate entries Using DISTINCT
ALL retains them - this is the default

SELECT ALL Last
FROM Student
SELECT DISTINCT Last
FROM Student

Слайд 6

WHERE Clauses

Usually you don’t want all the rows
A WHERE clause restricts the

WHERE Clauses Usually you don’t want all the rows A WHERE clause
rows that are returned
It takes the form of a condition - only those rows that satisfy the condition are returned

Example conditions:
Mark < 40
First = ‘John’
First <> ‘John’
First = Last
(First = ‘John’) AND
(Last = ‘Smith’)
(Mark < 40) OR (Mark > 70)

Слайд 7

WHERE Examples

SELECT * FROM Grade
WHERE Mark >= 60

SELECT DISTINCT ID

WHERE Examples SELECT * FROM Grade WHERE Mark >= 60 SELECT DISTINCT

FROM Grade
WHERE Mark >= 60
ID Code Mark
S103 DBS 72
S104 PR1 68
S104 IAI 65
S107 PR1 76
S107 PR2 60

Слайд 8

WHERE Example

Given the table

Write an SQL query to find a list of

WHERE Example Given the table Write an SQL query to find a
the ID numbers and marks in IAI of students who have passed (scored 40 or higher) IAI

ID Mark
S103 58
S104 65

Слайд 9

One Solution

SELECT ID, Mark FROM Grade
WHERE (Code = ‘IAI’) AND
(Mark

One Solution SELECT ID, Mark FROM Grade WHERE (Code = ‘IAI’) AND (Mark >= 40)
>= 40)

Слайд 10

SELECT from Multiple Tables

Often you need to combine information from two or

SELECT from Multiple Tables Often you need to combine information from two
more tables
You can get the effect of a product by using
SELECT * FROM Table1, Table2...

If the tables have columns with the same name ambiguity results
You resolve this by referencing columns with the table name
TableName.Column

Слайд 11

SELECT from Multiple Tables

SELECT
First, Last, Mark
FROM Student, Grade
WHERE
(Student.ID =

SELECT from Multiple Tables SELECT First, Last, Mark FROM Student, Grade WHERE

Grade.ID) AND
(Mark >= 40)

Слайд 12

SELECT from Multiple Tables

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

SELECT

SELECT from Multiple Tables ID First Last ID Code Mark S103 John
... FROM Student, Grade WHERE...

Слайд 13

SELECT from Multiple Tables

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

SELECT ... FROM

SELECT from Multiple Tables ID First Last ID Code Mark S103 John
Student, Grade
WHERE (Student.ID = Grade.ID) AND ...

Слайд 14

SELECT from Multiple Tables

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

SELECT from Multiple Tables ID First Last ID Code Mark S103 John
Student, Grade
WHERE (Student.ID = Grade.ID) AND (Mark >= 40)

Слайд 15

SELECT from Multiple Tables

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 First, Last,

SELECT from Multiple Tables First Last Mark John Smith 72 John Smith
Mark FROM Student, Grade
WHERE (Student.ID = Grade.ID) AND (Mark >= 40)

Слайд 16

SELECT from Multiple Tables

When selecting from multiple tables you almost always use

SELECT from Multiple Tables When selecting from multiple tables you almost always
a WHERE clause to find entries with common values

SELECT * FROM
Student, Grade, Course
WHERE
Student.ID = Grade.ID
AND
Course.Code =
Grade.Code

Слайд 17

SELECT from Multiple Tables

Student

Grade

Course

Student.ID = Grade.ID

Course.Code = Grade.Code

SELECT from Multiple Tables Student Grade Course Student.ID = Grade.ID Course.Code = Grade.Code

Слайд 18

JOINs

JOINs can be used to combine tables
There are many types of JOIN
CROSS

JOINs JOINs can be used to combine tables There are many types
JOIN
INNER JOIN
NATURAL JOIN
OUTER JOIN
OUTER JOINs are linked with NULLs - more later

A CROSS JOIN B
returns all pairs of rows from A and B
A NATURAL JOIN B
returns pairs of rows with common values for identically named columns and without duplicating columns
A INNER JOIN B
returns pairs of rows satisfying a condition

Слайд 19

CROSS JOIN

SELECT * FROM
Student CROSS JOIN
Enrolment

ID Name ID Code
123 John 123 DBS
124 Mary 123 DBS
125 Mark 123 DBS
126 Jane 123 DBS
123 John 124 PRG
124 Mary 124 PRG
125 Mark 124 PRG
126 Jane 124 PRG
123 John 124 DBS
124 Mary 124 DBS

CROSS JOIN SELECT * FROM Student CROSS JOIN Enrolment ID Name ID

Слайд 20

NATURAL JOIN

SELECT * FROM
Student NATURAL JOIN Enrolment

Code

DBS

PRG

DBS

PRG

NATURAL JOIN SELECT * FROM Student NATURAL JOIN Enrolment Code DBS PRG DBS PRG

Слайд 21

CROSS and NATURAL JOIN

SELECT * FROM
A CROSS JOIN B
is the

CROSS and NATURAL JOIN SELECT * FROM A CROSS JOIN B is
same as
SELECT * FROM A, B

SELECT * FROM
A NATURAL JOIN 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)

Слайд 22

INNER JOIN

INNER JOINs specify a condition which the pairs of rows satisfy
SELECT

INNER JOIN INNER JOINs specify a condition which the pairs of rows
* FROM
A INNER JOIN B
ON

Can also use
SELECT * FROM
A INNER JOIN B
USING
(col1, col2,…)
Chooses rows where the given columns are equal

Слайд 23

INNER JOIN

SELECT * FROM
Student INNER JOIN Enrolment USING (ID)

ID Name ID Code
123 John 123 DBS
124 Mary 124 PRG
124 Mary 124 DBS
126 Jane 126 PRG

INNER JOIN SELECT * FROM Student INNER JOIN Enrolment USING (ID) ID

Слайд 24

INNER JOIN

SELECT * FROM
Buyer INNER JOIN Property ON
Price <= Budget

Name Budget

INNER JOIN SELECT * FROM Buyer INNER JOIN Property ON Price Name
Address Price
Smith 100,000 15 High St 85,000
Jones 150,000 15 High St 85,000
Jones 150,000 12 Queen St 125,000

Слайд 25

INNER JOIN

SELECT * FROM
A INNER JOIN B
ON
is the

INNER JOIN SELECT * FROM A INNER JOIN B ON is the
same as
SELECT * FROM A, B
WHERE

SELECT * FROM
A INNER JOIN B
USING(col1, col2,...)
is the same as
SELECT * FROM A, B
WHERE A.col1 = B.col1
AND A.col2 = B.col2
AND ...

Слайд 26

JOINs vs WHERE Clauses

JOINs (so far) are not needed
You can have the

JOINs vs WHERE Clauses JOINs (so far) are not needed You can
same effect by selecting from multiple tables with an appropriate WHERE clause
So should you use JOINs or not?

Yes, because
They often lead to concise queries
NATURAL JOINs are very common
No, because
Support for JOINs varies a fair bit among SQL dialects

Слайд 27

Writing Queries

When writing queries
There are often many ways to write the query
You

Writing Queries When writing queries There are often many ways to write
should worry about being correct, clear, and concise in that order
Don’t worry about being clever or efficient

Most DBMSs have query optimisers
These take a user’s query and figure out how to efficiently execute it
A simple query is easier to optimise
We’ll look at some ways to improve efficiency later

Слайд 28

This Lecture in Exams

This Lecture in Exams

Слайд 29

This Lecture in Exams

Find a list of all the CD titles.
(1

This Lecture in Exams Find a list of all the CD titles.
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)
Имя файла: SQL-SELECT.pptx
Количество просмотров: 54
Количество скачиваний: 0