Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-310 Database Systems

Similar presentations


Presentation on theme: "CPSC-310 Database Systems"— Presentation transcript:

1 CPSC-310 Database Systems
Professor Jianer Chen Room 315C HRBB Lecture #13

2 SQL (continued) Basic Query Statement: SELECT attributes
FROM tables WHERE conditions The result of the query is a relation, which can be used in places where a regular relation appears.

3 Our Running Example Our SQL queries will be based on the following database schema (Underline indicates key attributes) Beers(name, manf) Bars(name, addr, license) Drinkers(name, addr, phone) Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar)

4 Subqueries A parenthesized SELECT-FROM- WHERE statement (subquery) can be used as a value in a number of places, including FROM and WHERE clauses.

5 Subqueries A parenthesized SELECT-FROM- WHERE statement (subquery) can be used as a value in a number of places, including FROM and WHERE clauses. Example: in place of a relation in the FROM clause, we can place another query, and then query its result.

6 Subqueries A parenthesized SELECT-FROM- WHERE statement (subquery) can be used as a value in a number of places, including FROM and WHERE clauses. Example: in place of a relation in the FROM clause, we can place another query, and then query its result. * Better use a tuple-variable to name tuples of the result.

7 Subqueries That Return One Tuple
If a subquery is guaranteed to produce one tuple, then the subquery can be used as a value. Usually, the tuple has one component. A run-time error occurs if there is no tuple or more than one tuple.

8 Query + Subquery Solution
Sells(bar, beer, price) Query + Subquery Solution SELECT bar FROM Sells WHERE beer = ’Miller’ AND price = (SELECT price WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’); The price at which Joe sells Bud

9 The IN Operator

10 The IN Operator <tuple> IN <relation> is true if and only if the tuple is a member of the relation.

11 The IN Operator <tuple> IN <relation> is true if and only if the tuple is a member of the relation. -- <tuple> NOT IN <relation> means the opposite.

12 The IN Operator <tuple> IN <relation> is true if and only if the tuple is a member of the relation. -- <tuple> NOT IN <relation> means the opposite. IN-expressions can appear in WHERE clauses.

13 The IN Operator <tuple> IN <relation> is true if and only if the tuple is a member of the relation. -- <tuple> NOT IN <relation> means the opposite. IN-expressions can appear in WHERE clauses. The <relation> is often a subquery.

14 The IN Operator <tuple> IN <relation> is true if and only if the tuple is a member of the relation. -- <tuple> NOT IN <relation> means the opposite. IN-expressions can appear in WHERE clauses. The <relation> is often a subquery. Example. From Beers(name, manf) and Likes(drinker, beer), find the name and manufacturer of each beer that Fred likes.

15 The IN Operator <tuple> IN <relation> is true if and only if the tuple is a member of the relation. -- <tuple> NOT IN <relation> means the opposite. IN-expressions can appear in WHERE clauses. The <relation> is often a subquery. Example. From Beers(name, manf) and Likes(drinker, beer), find the name and manufacturer of each beer that Fred likes. beers Fred likes (SELECT beer FROM Likes WHERE drinker = ’Fred’);

16 The IN Operator <tuple> IN <relation> is true if and only if the tuple is a member of the relation. -- <tuple> NOT IN <relation> means the opposite. IN-expressions can appear in WHERE clauses. The <relation> is often a subquery. Example. From Beers(name, manf) and Likes(drinker, beer), find the name and manufacturer of each beer that Fred likes. SELECT * FROM Beers WHERE name IN beers Fred likes (SELECT beer FROM Likes WHERE drinker = ’Fred’);

17 The IN Operator <tuple> IN <relation> is true if and only if the tuple is a member of the relation. -- <tuple> NOT IN <relation> means the opposite. IN-expressions can appear in WHERE clauses. The <relation> is often a subquery. Example. From Beers(name, manf) and Likes(drinker, beer), find the name and manufacturer of each beer that Fred likes. SELECT * FROM Beers WHERE name IN beers Fred likes (SELECT beer FROM Likes WHERE drinker = ’Fred’);

18 The IN Operator <tuple> IN <relation> is true if and only if the tuple is a member of the relation. -- <tuple> NOT IN <relation> means the opposite. IN-expressions can appear in WHERE clauses. The <relation> is often a subquery. Example. From Beers(name, manf) and Likes(drinker, beer), find the name and manufacturer of each beer that Fred likes. SELECT * FROM Beers WHERE name IN (SELECT beer FROM Likes WHERE drinker = ’Fred’);

19 The Exists Operator EXISTS(<relation>) is true if and only if the <relation> is not empty.

20 The Exists Operator EXISTS(<relation>) is true if and only if the <relation> is not empty. Example: From Beers(name, manf), find those beers that are the unique beer by their manufacturer.

21 The Exists Operator EXISTS(<relation>) is true if and only if the <relation> is not empty. Example: From Beers(name, manf), find those beers that are the unique beer by their manufacturer. SELECT name FROM Beers b1 WHERE NOT EXISTS (SELECT * FROM Beers WHERE manf = b1.manf AND name <> b1.name);

22 The Exists Operator EXISTS(<relation>) is true if and only if the <relation> is not empty. Example: From Beers(name, manf), find those beers that are the unique beer by their manufacturer. SELECT name FROM Beers b1 WHERE NOT EXISTS (SELECT * FROM Beers WHERE manf = b1.manf AND name <> b1.name); There should not be other beers that are made by the manf of b1

23 The Exists Operator EXISTS(<relation>) is true if and only if the <relation> is not empty. Example: From Beers(name, manf), find those beers that are the unique beer by their manufacturer. SELECT name FROM Beers b1 WHERE NOT EXISTS (SELECT * FROM Beers WHERE manf = b1.manf AND name <> b1.name); The set of beers that are not b1 but made by the same manf of b1 should be empty

24 The Exists Operator EXISTS(<relation>) is true if and only if the <relation> is not empty. Example: From Beers(name, manf), find those beers that are the unique beer by their manufacturer. SELECT name FROM Beers b1 WHERE NOT EXISTS (SELECT * FROM Beers WHERE manf = b1.manf AND name <> b1.name); The set of beers that are not b1 but made by the same manf of b1 should be empty

25 The Exists Operator EXISTS(<relation>) is true if and only if the <relation> is not empty. Example: From Beers(name, manf), find those beers that are the unique beer by their manufacturer. SELECT name FROM Beers b1 WHERE NOT EXISTS (SELECT * FROM Beers WHERE manf = b1.manf AND name <> b1.name); The set of beers that are not b1 but made by the same manf of b1 should be empty

26 The Exists Operator EXISTS(<relation>) is true if and only if the <relation> is not empty. Example: From Beers(name, manf), find those beers that are the unique beer by their manufacturer. Notice scope rule: manf refers to closest nested FROM with a relation having that attribute. SELECT name FROM Beers b1 WHERE NOT EXISTS (SELECT * FROM Beers WHERE manf = b1.manf AND name <> b1.name); The set of beers that are not b1 but made by the same manf of b1 should be empty

27 The Exists Operator EXISTS(<relation>) is true if and only if the <relation> is not empty. Example: From Beers(name, manf), find those beers that are the unique beer by their manufacturer. Notice scope rule: manf refers to closest nested FROM with a relation having that attribute. SELECT name FROM Beers b1 WHERE NOT EXISTS (SELECT * FROM Beers WHERE manf = b1.manf AND name <> b1.name); The set of beers that are not b1 but made by the same manf of b1 should be empty Notice the SQL “not equals” operator

28 The Exists Operator EXISTS(<relation>) is true if and only if the <relation> is not empty. Example: From Beers(name, manf), find those beers that are the unique beer by their manufacturer. SELECT name FROM Beers b1 WHERE NOT EXISTS (SELECT * FROM Beers WHERE manf = b1.manf AND name <> b1.name);

29 The Operators ANY and ALL

30 The Operators ANY and ALL
x = ANY(<relation>) is true if x equals at least one tuple in the relation.

31 The Operators ANY and ALL
x = ANY(<relation>) is true if x equals at least one tuple in the relation. = can be any other comparison operators.

32 The Operators ANY and ALL
x = ANY(<relation>) is true if x equals at least one tuple in the relation. = can be any other comparison operators. -- Example: x >= ANY(<relation>) means x is not smaller than the smallest tuple in the relation.

33 The Operators ANY and ALL
x = ANY(<relation>) is true if x equals at least one tuple in the relation. = can be any other comparison operators. -- Example: x >= ANY(<relation>) means x is not smaller than the smallest tuple in the relation. -- Here, tuples must have only one component.

34 The Operators ANY and ALL
x = ANY(<relation>) is true if x equals at least one tuple in the relation. = can be any other comparison operators. -- Example: x >= ANY(<relation>) means x is not smaller than the smallest tuple in the relation. -- Here, tuples must have only one component. x <> ALL(<relation>) is true if for every tuple t in the relation, x is not equal to t. -- That is, x is not a member of the relation.

35 The Operators ANY and ALL
x = ANY(<relation>) is true if x equals at least one tuple in the relation. = can be any other comparison operators. -- Example: x >= ANY(<relation>) means x is not smaller than the smallest tuple in the relation. -- Here, tuples must have only one component. x <> ALL(<relation>) is true if for every tuple t in the relation, x is not equal to t. -- That is, x is not a member of the relation. <> can be any other comparison operator.

36 The Operators ANY and ALL
x = ANY(<relation>) is true if x equals at least one tuple in the relation. = can be any other comparison operators. -- Example: x >= ANY(<relation>) means x is not smaller than the smallest tuple in the relation. -- Here, tuples must have only one component. x <> ALL(<relation>) is true if for every tuple t in the relation, x is not equal to t. -- That is, x is not a member of the relation. <> can be any other comparison operator. -- Example: x >= ALL(<relation>) means there is no tuple larger than x in the relation

37 Example From Sells(bar, beer, price), find the beer(s) sold for the highest price.

38 Example From Sells(bar, beer, price), find the beer(s) sold for the highest price. SELECT price FROM Sells collect all beer prices

39 Example From Sells(bar, beer, price), find the beer(s) sold for the highest price. SELECT beer FROM Sells WHERE price >= ALL( ); SELECT price FROM Sells select the beers whose price is not smaller than any price collect all beer prices

40 Example From Sells(bar, beer, price), find the beer(s) sold for the highest price. SELECT beer FROM Sells WHERE price >= ALL( ); SELECT price FROM Sells

41 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)

42 Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar) Example From relations Likes, Sells, and Frequents, find the drinkers and beers such that: The drinker likes the beer, and The drinker frequents at least one bar that sells the beer.

43 Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar) Example From relations Likes, Sells, and Frequents, find the drinkers and beers such that: The drinker likes the beer, and The drinker frequents at least one bar that sells the beer. the drinker likes the beer. (SELECT * FROM Likes)

44 Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar) Example From relations Likes, Sells, and Frequents, find the drinkers and beers such that: The drinker likes the beer, and The drinker frequents at least one bar that sells the beer. the drinker likes the beer. (SELECT * FROM Likes) INTERSECT (SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar ); The drinker frequents a bar that sells the beer.

45 Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar) Example From relations Likes, Sells, and Frequents, find the drinkers and beers such that: The drinker likes the beer, and The drinker frequents at least one bar that sells the beer. the drinker likes the beer. (SELECT * FROM Likes) INTERSECT (SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar ); The drinker frequents a bar that sells the beer.

46 Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar) Example From relations Likes, Sells, and Frequents, find the drinkers and beers such that: The drinker likes the beer, and The drinker frequents at least one bar that sells the beer. (SELECT * FROM Likes) INTERSECT (SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar );

47 Bag Semantics

48 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

49 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 Why?

50 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 Why?  for efficiency

51 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 Why?  for efficiency When doing projection, it is easier to avoid eliminating duplicates. -- Just work tuple-at-a-time.

52 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 Why?  for 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.

53 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 Why?  for 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. -- So you may as well eliminate the duplicates anyway.

54 Controlling Duplicate Elimination
Force the result to be a set by SELECT DISTINCT . . .

55 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 . . .

56 Example: DISTINCT From Sells(bar, beer, price), find all the different prices charged for beers:

57 Example: DISTINCT From Sells(bar, beer, price), find all the different prices charged for beers: SELECT DISTINCT price FROM Sells;

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

59 Example: ALL Using relations Frequents(drinker, bar) and Likes(drinker, beer), list drinkers who frequent more bars than they like beers, and does so as many times as the difference of those counts.

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

61 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.

62 Products and Natural Joins
Cross join (Cartesian Product): R CROSS JOIN S;

63 Products and Natural Joins
Cross join (Cartesian Product): R CROSS JOIN S; A B 2 3 4 CROSS JOIN B C D 5 6 R S A R.B S.B C D

64 Products and Natural Joins
Natural join (join tuples agreeing on common attributes): R NATURAL JOIN S;

65 Products and Natural Joins
Natural join (join tuples agreeing on common attributes): R NATURAL JOIN S; A B 2 3 4 NATURAL JOIN B C D 5 6 R S A B C D

66 Theta Join R JOIN S ON <condition>

67 Theta Join R JOIN S ON <condition> R S A B B C D 2 5 6 4 5 JOIN
4 5 JOIN B C D 5 6 R S A R.B S.B C D ON A < D;

68 Theta Join R JOIN S ON <condition>
Drinkers(name, addr) Frequents(drinker, bar) Theta Join R JOIN S ON <condition> Example: using Drinkers and Frequents: Drinkers JOIN Frequents ON name = drinker; gives all (d, a, d, b) quadruples such that drinker d lives at address a and frequents bar b. A B 2 4 5 JOIN B C D 5 6 R S A R.B S.B C D ON A < D;

69 Outerjoins R OUTER JOIN S is the core of an outerjoin expression. It is modified by: Optional NATURAL in front of OUTER. Optional ON <condition> after JOIN. 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.

70 Outerjoins (Examples)
R NATURAL FULL OUTER JOIN S R NATURAL LEFT OUTER JOIN S R NATURAL RIGHT OUTER JOIN S

71 Outerjoins (Examples)
R NATURAL FULL OUTER JOIN S A B 2 3 9 B C D 5 6 R NATURAL FULL OUTER JOIN S A B C D N N N

72 Outerjoins (Examples)
R NATURAL LEFT OUTER JOIN S A B 2 3 9 B C D 5 6 R NATURAL LEFT OUTER JOIN S A B C D N N

73 Outerjoins (Examples)
R NATURAL RIGHT OUTER JOIN S A B 2 3 9 B C D 5 6 R NATURAL RIGHT OUTER JOIN S A B C D N


Download ppt "CPSC-310 Database Systems"

Similar presentations


Ads by Google