sql part i: standard queries. comp-421: database systems - sql queries i 2 example instances sid...

20
SQL Part I: Standard Queries

Upload: james-lawson

Post on 03-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

SQLPart I: Standard

Queries

Page 2: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

2COMP-421: Database Systems - SQL Queries I

Example Instances

sid sname

rating age22 debby 7

3531 debby 8 5558 lilly 10 35

Sailors

Reserves RS

sid bid day

22 101 2005/24/07

58 103 2005/07/30

59 103 2005/07/28

58 103

2005/07/31

bid bnamecolor

101Interlake green103 Snapper red

Boat B

Page 3: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

3COMP-421: Database Systems - SQL Queries I

Principle Form of a Query SELECT desired attributes

FROM list of relationsWHERE qualification(where clause is optional)

SELECT rating,ageFROM SailorsWHERE rating <= 6 OR age < 40

Operational Semantics Imagine a tuple variable ranging over all tuples of the

relation For each tuple: check if is satisfies the WHERE clause. If

so, print the attributes in SELECT. Conversion to Relational Algebra

rating,age (rating<=6 age<40 (Sailors)) Start with the relation in the FROM clause Apply , using condition in WHERE clause (selection) Apply ∏, using attributes in SELECT clause (projection) Difference: duplicates not eliminated

sid sname

rating age22 debby 7

3531 debby 8 5558 lilly 10 35

rating age

7 3510 35

Page 4: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

4COMP-421: Database Systems - SQL Queries I

The WHERE Clause Comparison:

attr1 op const, or attr1 op attr2, op is one of , , , , , , LIKE We may apply the usual arithmetic operations +, *, etc.

to numeric values before we compare Qualification/Condition: Comparisons combined using

AND, OR and NOT name =‘Cheng’ AND NOT age = 18 name LIKE ‘%e_g’ (%: any string, _:any character) Further string operations, e.g., concatenation, string-

length, etc. As default for most statements, SQL uses ‘multiset’

semantic, i.e., duplicates are allowed and not eliminated (as long as they do not violate a primary key / unique constraint)

DISTINCT is an optional keyword indicating that the answer should not contain duplicates.

Page 5: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

5COMP-421: Database Systems - SQL Queries I

Attribute Lists

DistinctSELECT DISTINCT snameFROM Sailors

(no WHERE clause OK)

Star as list of all attributesSELECT *FROM SailorsWHERE rating < 9

Renaming columnsSELECT sid, sname AS sailorFROM SailorsWHERE rating < 9

sid sname

rating age

22 debby 7 3531 debby 8 55

sailor22

debby 31 debby

snamedebby lilly

sid

sid sname

rating

age22 debby 7 3531 debby 8 5558 lilly 10 35

Page 6: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

6COMP-421: Database Systems - SQL Queries I

Attribute Lists (contd) Expressions as values in columns

SELECT sname, rating+1 AS upgradeFROM Sailors

Constants as attribute valuesSELECT rating AS reality, ‘10’ AS dreamFROM SailorsWHERE sname LIKE ‘_e%y’

Ordered OutputSELECT *FROM SailorsORDER BY age,rating

sname upgradedebby 8debby 9 lilly 11

reality dream 7 10 8 10

sid sname

rating age

22 debby 7 35

58 lilly 10 35

31 debby 8 55

Page 7: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

7COMP-421: Database Systems - SQL Queries I

Multirelational Queries

List of relations in FROM clause Relation-dot-attribute disambiguates attributes from

several relations. Cross-Product:

SELECT *FROM Sailors, ReservesSailors X Reserves

Join: Have to indicate comparison even with natural join Example: “give me the names of all sailors that have

reserved boat #103”SELECT snameFROM Sailors, ReservesWHERE Sailors.sid = Reserves.sid AND bid = 103

sname (bid=103 (Reserves) Sailors)

Page 8: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

8COMP-421: Database Systems - SQL Queries I

Semantics Conversion to Relational Algebra

Same as for single relation, but start with the product (X) of all the relations mentioned in the FROM clause

Parallel assignment of tuple variables Consider a tuple variable for each relation in the FROM

Consider all possible assignments For each such assignment of tuple variables to tuples that makes the WHERE true, output the attributes of the SELECTsid snam

erating age22 debby 7

3531 debby 8 5558 lilly 10 35

sid bid day

22 101 2005/24/07

58 103 2005/07/30

59 103 2005/07/28

58 103

2005/07/31

sname

debby Lillylilly

Page 9: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

9COMP-421: Database Systems - SQL Queries I

Semantics (contd).

Nested Loop AssignmentLET the tuple variables in the from clause range over relations R1, R2, …., RnFOR each tuple t1 in relation R1 DO

FOR each tuple t2 in relation R2 DO…FOR each tuple tn in relation Rn DO

If the where clause is satisfied when the values from t1, t2, … tn are substituted for all attribute references THEN evaluate the attributes of the

select clause according to t1, t2, …. tn and produce the tuple of values that results.

Page 10: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

10COMP-421: Database Systems - SQL Queries I

Range Variables

Optional use of range variables

SELECT S.sname

FROM Sailors S, Reserves R

WHERE S.sid = R.sid AND R.bid = 103 Use of range variable required when the same

relation appears twice in the FROM clause Example: “find pairs of sailors that have rented the

same boat”

SELECT r1.sid, r2.sid

FROM Reserves r1, Reserves r2

WHERE r1.bid = r2.bid AND r1.sid < r2.sid

(note that r1.sid < r2.sid is needed to avoid producing (58,58) and to avoid producing a pair in both directions.

Page 11: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

11COMP-421: Database Systems - SQL Queries I

Union, Intersection, Difference

Input relations for set operators must be set-compatible, I.e. they must haveSame number of attributesThe attributes, taken in order, must have same type

As default, result relation is a set!!! (no multiset)

Many systems do not provide primitives for intersection and difference

Page 12: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

12COMP-421: Database Systems - SQL Queries I

Union

Sailors(sid,sname,rating,age)Reserves(sid,bid,day)Boats(bid,bname,color)

Find sailors that have reserved a red or a green boatSELECT R.sidFROM Reserves R, Boats BWHERE R.bid = B.bid AND

(B.color = ‘red’ OR B.color = ‘green’)

SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘red’ UNION SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘green’

Page 13: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

13COMP-421: Database Systems - SQL Queries I

Intersection Find sailors that have reserved a red and a green boat(1) SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘red’ INTERSECT SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘green’

(2) SELECT R1.sidFROM Reserves R1, Reserves R2, Boats B1, Boats B2WHERE (R1.bid = B1.bid AND B1.color = ‘red’) AND

(R2.bid = B2.bid AND B2.color = ‘green’) AND R1.sid = R2.sid)

sid bid day

22 101 2005/24/07

58 103 2005/07/30

22 103

2005/07/28

58 103

2005/07/31

sid bid day

22 101 2005/24/07

58 103 2005/07/30

22 103

2005/07/28

58 103

2005/07/31

bidcolor101

green

103 red

bidcolor

101 green

103 red

Page 14: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

14COMP-421: Database Systems - SQL Queries I

Difference

Find sailors that have reserved a red but not a green boat

SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘red’ EXCEPT (Oracle: MINUS) SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘green’

Page 15: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

15COMP-421: Database Systems - SQL Queries I

Multiset Semantic

A multiset (bag) may contain the same tuple more than once, although there is no specified order (unlike a list). Example: {1, 2, 1, 3} is a multiset, but not a set

Multiset Union: Sum the times an element appears in the two multisets Example: {1, 2, 2} {1, 2, 3, 3} = {1, 1, 2, 2, 2, 3, 3}

Multiset Intersection: Take the minimum of the number of occurrences in each

multiset. Example: {1, 2, 2} {1, 1, 2, 2, 3, 3} = {1, 2,2}

Multiset Difference: Subtract the number of occurrences in the two multisets Examples: {1, 2, 2} - {1, 2, 3, 3} = {2}

Some familiar laws for sets also hold for multisets (e.g., union is commutative); but other laws do not hold (e.g., R (S T) (R S) (R T)

Page 16: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

16COMP-421: Database Systems - SQL Queries I

Multiset Semantic in SQL

Although SQL generally works with multisets, it uses set semantic for union/intersection/difference

To enforce multiset semantic for these operators use UNION ALL, INTERSECT ALL, EXCEPT ALL

SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘red’ UNION ALL SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘green’

Page 17: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

17COMP-421: Database Systems - SQL Queries I

Nested queries: The IN operator

A where clause can itself contain an SQL query. The inner query is called a subquery

Find names of sailors who have reserved boat #103SELECT S.sname

FROM Sailors S WHERE S.sid IN (SELECT R.sid

FROM Reserves R WHERE R.bid = 103)

To find sailors who have NOT reserved boat #103 use NOT IN

Semantics best understood by nested loop assignment

Multiple attributes: WHERE (a1,a2) IN (SELECT a3, a4…

Page 18: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

18COMP-421: Database Systems - SQL Queries I

Exists Operator

EXISTS (relation) is true iff the relation is non-empty

Find names of sailors who have reserved boat #103SELECT S.sname

FROM Sailors S WHERE EXISTS (SELECT *

FROM Reserves R WHERE R.bid = 103 AND R.sid = S.sid)

Scoping rule: to refer to outer Sailors in the inner subquery, we need to give a range variable to the outer relation.

A subquery that refers to values from a surrounding query is called a correlated subquery.

Since the inner query depends on the row of the outer query it must be reevaluated for each row in the outer query

Page 19: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

19COMP-421: Database Systems - SQL Queries I

Quantifiers ANY and ALL behave as existential and universal quantifiers, respectively.

Syntax WHERE attr op ANY (SELECT … WHERE attr op ALL (SELECT op is one of , , , , ,

Find the sailors with the highest ratingSELECT *

FROM Sailors WHERE rating ALL (SELECT rating

FROM Sailors)

Page 20: SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly

20COMP-421: Database Systems - SQL Queries I

Complex queries (Division)

Find sailors who have reserved all boatsSELECT sname

FROM Sailors S WHERE NOT EXISTS ((SELECT B.bid FROM Boats B)

EXCEPT (SELECT R.bid

FROM Reserves R WHERE R.sid=S.sid))

SELECT sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid

FROM Boats B WHERE NOT EXISTS (SELECT R.bid

FROM Reserves R WHERE R.bid = B.bid

AND R.sid =

S.sid))