SQL: The Query Language: R &G - Chapter 5
SQL: The Query Language: R &G - Chapter 5
R &G - Chapter 5
Example Database
Sailors
sid 1 2 3 sname Fred Jim Nancy rating 7 2 8 age 22 39 27
Boats
bid 101 102 103 bname Nina Pinta color red blue
Reserves
sid 1 2 bid 102 102 day 9/12 9/13
Sailors
sid 1 2 3 sname Fred Jim Nancy rating 7 2 8 age 22 39 27
Sailors
sid 1 2 3 sname Fred Jim Nancy rating 7 2 8 age 22 39 27
Reserves
sid 1 2 bid 102 102 day 9/12 9/13
SELECT
[DISTINCT]
target-list
FROM
relation-
list
relation-list : List of relation names qualification possibly with a range variable after each name target-list : List of attributes of tables in relation-list qualification : Comparisons combined using AND, OR and NOT. DISTINCT : optional keyword indicating that the answer should not contain duplicates.
WHERE
Query Semantics
1. 2. 3. 4. FROM : compute cross product of tables. WHERE : Check conditions, discard tuples that fail. SELECT : Delete unwanted fields. DISTINCT (optional) : eliminate duplicate rows.
Note: Probably the least efficient way to compute a query! Query optimizer will find more efficient ways to get the same answer.
SELECT x.sname, x.age, y.sname, y.age FROM Sailors x, Sailors y WHERE x.age > y.age
Sailors
sid 1 2 3 sname Fred Jim Nancy rating 7 2 8 age 22 39 27
Arithmetic Expressions
SELECT S.age, S.age-5 AS age1, 2*S.age AS age2 FROM Sailors S WHERE S.sname = dustin
SELECT S1.sname AS name1, S2.sname AS name2 FROM Sailors S1, Sailors S2 WHERE 2*S1.rating = S2.rating - 1
String Comparisons
`_ stands for any one character and `% stands for 0 or more arbitrary characters.
Find sids of sailors whove reserved a red or a green boat SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND (B.color=red OR B.color=green)
... or:
SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=red UNION SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND
SELECT R.sid FROM Boats B,Reserves R WHERE R.bid=B.bid AND (B.color=red AND B.color=green)
SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=red INTERSECT SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=green
SELECT R1.sid FROM Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE R1.sid=R2.sid AND R1.bid=B1.bid AND R2.bid=B2.bid AND (B1.color=red AND B2.color=gre
SELECT S.sid FROM Sailors S EXCEPT SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid
Nested Queries: IN
Reserv
WHERE
SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Subquery must be recomputed for each Sailors tuple. Think of subquery as a function call that runs a query! Also: NOT EXISTS.
UNIQUE
SELECT S.sname FROM Sailors S WHERE UNIQUE (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid)
A Tough One
there is no boat B FROM Boats B without ... WHERE NOT EXISTS (SELECT R.bid a Reserves tuple showing S reserved B
Summary
Relational model has well-defined query semantics SQL provides functionality close to basic relational model (some differences in duplicate handling, null values, set operators, ) Typically, many ways to write a query DBMS figures out a fast way to execute a query, regardless of how it is written.