SQL Select: Database Systems Lecture 7
SQL Select: Database Systems Lecture 7
• Starting SQL
• DROP TABLE
• ALTER TABLE
• INSERT, UPDATE, and DELETE
• Sequences
• SQL SELECT
• WHERE clauses
• SELECT from multiple tables
• JOINs
• Aliases
• Virtual Relations:
Tables generated in the ether, derived by an SQL
operation on base relations and other virtual
relations.
• Views:
Virtual relations whose definition is stored by the
DBMS.
Different Sections of SQL
• The Data Definition Language (DDL):
Some examples:
DESCRIBE – list the structure of a table
• Example:
SQL> DESCRIBE Student;
Name Null? Type
------------ -------- ----------
STUID NOT NULL NUMBER(38)
STUNAME NOT NULL VARCHAR2(50)
STUADDRESS VARCHAR2(50)
STUYEAR NUMBER(38)
Oracle Data Dictionary
• To find out what tables and sequences you
have defined use:
ID
S103
S104
S105
S106
S107
Sample SELECTs
SELECT * FROM Student
ID FirstLast
S103 John Smith
S104 Mary Jones
S105 Jane Brown
S106 Mark Jones
S107 John Brown
DISTINCT and ALL
• Sometimes you end up SELECT Last
with duplicate
ALL Lastentries
FROM Student Smith
Jones
• Using DISTINCT removes duplicates Brown
Jones
Brown
• Using ALL retains them - this is the default
SELECT DISTINCT Last
FROM Student Last
Smith
Jones
Brown
WHERE Clauses
• Usually you don’t want all
• Example
the rowsconditions:
• Mark < 40
• First
• A WHERE clause restricts the rows that =are
‘John’
returned
• First <> ‘John’
• First = Last
• It takes the form of a condition - only those rows
• (First = ‘John’)
that satisfy the condition are returned
AND
(Last = ‘Smith’)
• (Mark < 40) OR
(Mark > 70)
WHERE Examples
SELECT DISTINCT
* FROM Grade
ID
WHERE
FROM Mark
Grade>= 60
WHERE Mark >= 60
ID Code Mark
S103 DBS 72 ID
S104 PR168 S103
S104 IAI 65 S104
S107 PR176 S107
S107 PR260
WHERE Example
Givenanthe
•• Write SQLtable
query to find a list of the ID numbers and
marks in IAI of students who have passed (scored 40 or
higher)
Grade IAI
ID Code Mark
S103 DBS 72
S103 IAI 58
S104 PR168
S104 IAI 65
S106 PR243 ID Mark
S107 PR176
S103 58
S107 PR260
S104 65
S107 IAI 35
One Solution
We only want the ID and Mark, not the Code
Single quotes around the string
TableName.Column
SELECT from Multiple Tables
Student
SELECT
ID FirstLast
First, Last, Mark
S103 John Smith
FROM Student, Grade S104 Mary Jones
WHERE S105 Jane Grade
Brown
S106 Mark ID Jones
Code Mark
(Student.ID = S107 John Brown
S103 DBS 72
Grade.ID) AND S103 IAI 58
(Mark >= 40) S104 PR168
S104 IAI 65
S106 PR243
S107 PR176
S107 PR260
S107 IAI 35
SELECT from Multiple Tables
SELECT ... FROM Student, Grade WHERE...
Student.ID Grade.ID
SELECT from Multiple Tables
SELECT ... FROM Student, Grade
WHERE (Student.ID = Grade.ID) AND (Mark >= 40)
FirstLast 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
•SELECT FROM from multiple tables you
almost always
Student, Grade, use a WHERE clause to find
Course
entries with common values
WHERE
Student.ID = Grade.ID
AND
• You have joined tables by listing them with
Course.Code =
commas.
Grade.Code
SELECT from Multiple Tables
Student Grade Course
• No, because
• Support for JOINs varies a fair bit among SQL
dialects – it can be inconsistent.
Writing Queries
• Most
WhenDBMSs
writinghave
queries
query optimisers
• These are
There takeoften
a users
manyquery
waysandto figure
write the
out query
how to
• efficiently
You shouldexecute it
worry about being correct, clear, and
• concise
A simpleinquery
that order
is easier to optimise
• Don’t look
We’ll worryat about
some being
ways to
clever
improve
or efficient
efficiency later
Multiple Joins Example
Work
Act Direct
Actor Director
Name Sex Name Age
Al Francis
Al M Uma Quentin Francis 67
Uma F Jon Quentin Quentin 43
Jon M Jon Steven Steven 60
Samuel M Samuel Quentin Woody 71
Robert M Robert Francis
Harvey M Harvey Quentin
Scarlet F Harvey Francis
Multiple Join Query
• What is the sex of the actors who have
worked with directors under the age of 65?
Work
Act Direct
Actor Director
Name Sex Name Age
Al Francis
Al M Uma Quentin Francis 67
Uma F Jon Quentin Quentin 43
Jon M Jon Steven Steven 60
Samuel M Samuel Quentin Woody 71
Robert M Robert Francis
Harvey M Harvey Quentin
Scarlet F Harvey Francis
Solution
• What is the sex of the actors who have
worked with directors under the age of 65?
• Table alias
SELECT ...
FROM table
AS newName
SQL says ‘AS’ is optional, but
Oracle doesn’t even accept it.
Business Alias Example
Employees
ID Name SELECT
123 Tinky Winky E.ID AS empID,
124 Dipsy
E.Name, W.Dept
125 La La
126 Po FROM
Employee E
WorksIn
WorksIn W
ID Dept
WHERE
124 Marketing E.ID = W.ID
125 Sales
125 Marketing
Business Alias Example
SELECT
result: E.ID AS empID,
ID Name Dept
E.Name, W.Dept
FROM
124 Dipsy Marketing Employee E
125 La La Sales
125 La La Marketing WorksIn W
WHERE
E.ID = W.ID
This Lecture in Exams
Track CD
cID Num Title Time aID cID Title Price
1 1 Violent 2391 1 Mix 9.99
1 2 Every Girl 4101 2 Compilation 12.99
2 3 Breather 2171
1 4 Part of Me 2791 Artist
2 1 Star 3621
2 2 Teaboy 4172 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)