Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 6 Introduction to SQL part 4 Slides from

Similar presentations


Presentation on theme: "1 Lecture 6 Introduction to SQL part 4 Slides from"— Presentation transcript:

1 1 Lecture 6 Introduction to SQL part 4 Slides from http://infolab.stanford.edu/~ullman/fcdb/aut07/index.html#lecturehttp://infolab.stanford.edu/~ullman/fcdb/aut07/index.html#lecture CS4416 Database Systems CS5122 Development of IS 2

2 2 Union, Intersection, and Difference uUnion, intersection, and difference of relations are expressed by the following forms, each involving subqueries: w( ) UNION ( ) w( ) INTERSECT ( ) w( ) EXCEPT ( )

3 3 Example: Intersection uUsing Likes(drinker, beer), Sells(bar, beer, price), and Frequents(drinker, bar), find the drinkers and beers such that: 1.The drinker likes the beer, and 2.The drinker frequents at least one bar that sells the beer.

4 4 Solution (SELECT * FROM Likes) INTERSECT (SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar ); The drinker frequents a bar that sells the beer. Notice trick: subquery is really a stored table.

5 5 Bag Semantics uAlthough the SELECT-FROM-WHERE statement uses bag semantics, the default for union, intersection, and difference is set semantics. wThat is, duplicates are eliminated as the operation is applied.

6 6 Motivation: Efficiency uWhen doing projection, it is easier to avoid eliminating duplicates. wJust work tuple-at-a-time. uFor intersection or difference, it is most efficient to sort the relations first. wAt that point you may as well eliminate the duplicates anyway.

7 7 Controlling Duplicate Elimination uForce the result to be a set by SELECT DISTINCT... uForce the result to be a bag (i.e., don’t eliminate duplicates) by ALL, as in... UNION ALL...

8 8 Example: DISTINCT uFrom Sells(bar, beer, price), find all the different prices charged for beers: SELECT DISTINCT price FROM Sells; uNotice that without DISTINCT, each price would be listed as many times as there were bar/beer pairs at that price.

9 9 Example: ALL uUsing relations Frequents(drinker, bar) and Likes(drinker, beer): (SELECT drinker FROM Frequents) EXCEPT ALL (SELECT drinker FROM Likes); uLists drinkers who frequent more bars than they like beers, and does so as many times as the difference of those counts.

10 10 Join Expressions uSQL provides several versions of (bag) joins. uThese expressions can be stand-alone queries or used in place of relations in a FROM clause.

11 11 Products and Natural Joins uNatural join: R NATURAL JOIN S; uProduct: R CROSS JOIN S; uExample: Likes NATURAL JOIN Sells; uRelations can be parenthesized subqueries, as well.

12 12 Theta Join uR JOIN S ON uExample: using Drinkers(name, addr) and Frequents(drinker, bar): Drinkers JOIN Frequents ON name = drinker; gives us all (d, a, d, b) quadruples such that drinker d lives at address a and frequents bar b.

13 13 Outerjoins uR OUTER JOIN S is the core of an outerjoin expression. It is modified by: 1.Optional NATURAL in front of OUTER. 2.Optional ON after JOIN. 3.Optional LEFT, RIGHT, or FULL before OUTER. uLEFT = pad dangling tuples of R only. uRIGHT = pad dangling tuples of S only. uFULL = pad both; this choice is the default. Only one of these See: http://en.wikipedia.org/wiki/Join_(SQL)http://en.wikipedia.org/wiki/Join_(SQL)

14 OREDER BY Clause uFormat wSELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC 14


Download ppt "1 Lecture 6 Introduction to SQL part 4 Slides from"

Similar presentations


Ads by Google