1 lecture 6 introduction to sql part 4 slides from
DESCRIPTION
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.TRANSCRIPT
![Page 1: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/1.jpg)
1
Lecture 6
Introduction to SQL part 4
Slides from http://infolab.stanford.edu/~ullman/fcdb/aut07/index.html#lecture
CS4416 Database SystemsCS5122 Development of IS 2
![Page 2: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/2.jpg)
2
Union, Intersection, and Difference
Union, intersection, and difference of relations are expressed by the following forms, each involving subqueries: (<subquery>) UNION (<subquery>) (<subquery>) INTERSECT
(<subquery>) (<subquery>) EXCEPT (<subquery>)
![Page 3: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/3.jpg)
3
Example: Intersection Using Likes(drinker, beer), Sells(bar,
beer, price), and Frequents(drinker, bar), find the drinkers and beers such that:
1. The drinker likes the beer, and2. The drinker frequents at least one bar
that sells the beer.
![Page 4: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/4.jpg)
4
Solution(SELECT * FROM Likes)
INTERSECT(SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar);
The drinker frequentsa bar that sells thebeer.
Notice trick:subquery isreally a storedtable.
![Page 5: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/5.jpg)
5
Bag Semantics Although the SELECT-FROM-
WHERE statement uses bag semantics, the default for union, intersection, and difference is set semantics. That is, duplicates are eliminated as
the operation is applied.
![Page 6: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/6.jpg)
6
Motivation: Efficiency When doing projection, it is easier
to avoid eliminating duplicates. Just work tuple-at-a-time.
For intersection or difference, it is most efficient to sort the relations first. At that point you may as well
eliminate the duplicates anyway.
![Page 7: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/7.jpg)
7
Controlling Duplicate Elimination
Force the result to be a set by SELECT DISTINCT . . .
Force the result to be a bag (i.e., don’t eliminate duplicates) by ALL, as in . . . UNION ALL . . .
![Page 8: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/8.jpg)
8
Example: DISTINCT From Sells(bar, beer, price), find all
the different prices charged for beers:SELECT DISTINCT priceFROM Sells;
Notice that without DISTINCT, each price would be listed as many times as there were bar/beer pairs at that price.
![Page 9: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/9.jpg)
9
Example: ALL Using relations Frequents(drinker, bar)
and Likes(drinker, beer):(SELECT drinker FROM Frequents)EXCEPT ALL(SELECT drinker FROM Likes);
Lists drinkers who frequent more bars than they like beers, and does so as many times as the difference of those counts.
![Page 10: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/10.jpg)
10
Join Expressions SQL provides several versions of
(bag) joins. These expressions can be stand-
alone queries or used in place of relations in a FROM clause.
![Page 11: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/11.jpg)
11
Products and Natural Joins Natural join:
R NATURAL JOIN S; Product:
R CROSS JOIN S; Example:
Likes NATURAL JOIN Sells; Relations can be parenthesized
subqueries, as well.
![Page 12: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/12.jpg)
12
Theta Join R JOIN S ON <condition> Example: 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.
![Page 13: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/13.jpg)
13
Outerjoins R OUTER JOIN S is the core of an
outerjoin expression. It is modified by:
1. Optional NATURAL in front of OUTER.2. Optional ON <condition> after JOIN.3. Optional LEFT, RIGHT, or FULL before
OUTER. LEFT = pad dangling tuples of R only. RIGHT = pad dangling tuples of S only. FULL = pad both; this choice is the default.
Only oneof these
See: http://en.wikipedia.org/wiki/Join_(SQL)
![Page 14: 1 Lecture 6 Introduction to SQL part 4 Slides from](https://reader036.vdocuments.mx/reader036/viewer/2022082908/5a4d1ad27f8b9ab059971e64/html5/thumbnails/14.jpg)
OREDER BY Clause Format
SELECT column_name(s)FROM table_nameORDER BY column_name(s) ASC|DESC
14