Slides are reused by the approval of Jeffrey Ullman’s

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

1 Introduction to SQL Select-From-Where Statements Multirelation Queries Subqueries.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
SQL CSET 3300.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
Subqueries Example Find the name of the producer of ‘Star Wars’.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #4.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
Chapter 6 Notes. 6.1 Simple Queries in SQL SQL is not usually used as a stand-alone language In practice there are hosting programs in a high-level language.
SQL 2014, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
Databases 1 Second lecture.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Views, Indexes Virtual and Materialized Views Speeding Accesses to Data.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
Introduction to SQL Introduction Select-From-Where Statements Queries over Several Relations Subqueries.
1 Lecture 6 Introduction to SQL part 4 Slides from
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
1 Introduction to Database Systems, CS420 SQL Views and Indexes.
1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation.
1 Data Modification with SQL CREATE TABLE, INSERT, DELETE, UPDATE Slides from
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
1 Database Design: DBS CB, 2 nd Edition SQL: Select-From-Where Statements & Multi-relation Queries & Subqueries Ch. 6.
Select-From-Where Statements Multirelation Queries Subqueries
CPSC-310 Database Systems
Virtual and Materialized Views Speeding Accesses to Data
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
CPSC-310 Database Systems
Computational Biology
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
CPSC-310 Database Systems
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Introduction to Database Systems, CS420
CPSC-608 Database Systems
06a: SQL-1 The Basics– Select-From-Where
CS 440 Database Management Systems
CPSC-608 Database Systems
Database Design and Programming
CPSC-310 Database Systems
CPSC-310 Database Systems
IST 210: Organization of Data
CPSC-310 Database Systems
IT 244 Database Management System
2018, Fall Pusan National University Ki-Joune Li
CMSC-461 Database Management Systems
Virtual and Materialized Views Speeding Accesses to Data
CPSC-608 Database Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CPSC-608 Database Systems
CPSC-608 Database Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

Slides are reused by the approval of Jeffrey Ullman’s SQL Subqueries Aggregation Views Slides are reused by the approval of Jeffrey Ullman’s

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 use a subquery and then query its result. Must use a tuple-variable to name tuples of the result.

Example: Subquery in FROM Find the beers liked by at least one person who frequents Joe’s Bar. SELECT beer FROM Likes, (SELECT drinker FROM Frequents WHERE bar = ’Joe’’s Bar’)JD WHERE Likes.drinker = JD.drinker; Drinkers who frequent Joe’s Bar

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.

Example: Single-Tuple Subquery Using Sells(bar, beer, price), find the bars that serve Miller for the same price Joe charges for Bud. Two queries would surely work: Find the price Joe charges for Bud. Find the bars that serve Miller at that 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

The IN Operator <tuple> IN (<subquery>) is true if and only if the tuple is a member of the relation produced by the subquery. Opposite: <tuple> NOT IN (<subquery>). IN-expressions can appear in WHERE clauses.

Example: IN Using 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’); The set of beers Fred likes

Remember These From Lecture #1? SELECT a FROM R, S WHERE R.b = S.b; FROM R WHERE b IN (SELECT b FROM S);

IN is a Predicate About R’s Tuples SELECT a FROM R WHERE b IN (SELECT b FROM S); Two 2’s One loop, over the tuples of R a b 1 2 3 4 R b c 2 5 2 6 S (1,2) satisfies the condition; 1 is output once.

This Query Pairs Tuples from R, S SELECT a FROM R, S WHERE R.b = S.b; Double loop, over the tuples of R and S a b 1 2 3 4 R b c 2 5 2 6 S (1,2) with (2,5) and (1,2) with (2,6) both satisfy the condition; 1 is output twice.

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

Example: EXISTS SELECT name FROM Beers b1 WHERE NOT EXISTS ( SELECT * WHERE manf = b1.manf AND name <> b1.name); Notice scope rule: manf refers to closest nested FROM with a relation having that attribute. Set of beers with the same manf as b1, but not the beer Notice the SQL “not equals” operator

The Operator ANY x = ANY(<subquery>) is a boolean condition that is true iff x equals at least one tuple in the subquery result. = could be any comparison operator. Example: x >= ANY(<subquery>) means x is not the uniquely smallest tuple produced by the subquery. Note tuples must have one component only.

The Operator ALL x <> ALL(<subquery>) is true iff for every tuple t in the relation, x is not equal to t. That is, x is not in the subquery result. <> can be any comparison operator. Example: x >= ALL(<subquery>) means there is no tuple larger than x in the subquery result.

Example: ALL 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); price from the outer Sells must not be less than any price.

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

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

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

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.

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.

Products and Natural Joins R NATURAL JOIN S; Product: R CROSS JOIN S; Example: Likes NATURAL JOIN Sells; Relations can be parenthesized subqueries, as well.

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.

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.

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.

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

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.

Aggregations SUM, AVG, COUNT, MIN, and MAX can be applied to a column in a SELECT clause to produce that aggregation on the column. Also, COUNT(*) counts the number of tuples.

Example: Aggregation From Sells(bar, beer, price), find the average price of Bud: SELECT AVG(price) FROM Sells WHERE beer = ’Bud’;

NULL’s Ignored in Aggregation NULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column. But if there are no non-NULL values in a column, then the result of the aggregation is NULL. Exception: COUNT of an empty set is 0.

Example: Effect of NULL’s SELECT count(*) FROM Sells WHERE beer = ’Bud’; SELECT count(price) The number of bars that sell Bud. The number of bars that sell Bud at a known price.

Grouping We may follow a SELECT-FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results from the SELECT-FROM-WHERE is grouped according to the values of all those attributes, and any aggregation is applied only within each group.

Example: Grouping From Sells(bar, beer, price), find the average price for each beer: SELECT beer, AVG(price) FROM Sells GROUP BY beer; beer AVG(price) Bud 2.33 … …

Example: Grouping From Sells(bar, beer, price) and Frequents(drinker, bar), find for each drinker the average price of Bud at the bars they frequent: SELECT drinker, AVG(price) FROM Frequents, Sells WHERE beer = ’Bud’ AND Frequents.bar = Sells.bar GROUP BY drinker; Compute all drinker-bar- price triples for Bud. Then group them by drinker.

Restriction on SELECT Lists With Aggregation If any aggregation is used, then each element of the SELECT list must be either: Aggregated, or An attribute on the GROUP BY list.

HAVING Clauses HAVING <condition> may follow a GROUP BY clause. If so, the condition applies to each group, and groups not satisfying the condition are eliminated.

Example: HAVING From Sells(bar, beer, price) and Beers(name, manf), find the average price of those beers that are either served in at least three bars or are manufactured by Pete’s.

Solution SELECT beer, AVG(price) FROM Sells GROUP BY beer Beer groups with at least 3 non-NULL bars and also beer groups where the manufacturer is Pete’s. SELECT beer, AVG(price) FROM Sells GROUP BY beer HAVING COUNT(bar) >= 3 OR beer IN (SELECT name FROM Beers WHERE manf = ’Pete’’s’); Beers manu- factured by Pete’s.

Requirements on HAVING Conditions Anything goes in a subquery. Outside subqueries, they may refer to attributes only if they are either: A grouping attribute, or Aggregated (same condition as for SELECT clauses with aggregation).

Views A view is a relation defined in terms of stored tables (called base tables ) and other views. Two kinds: Virtual = not stored in the database; just a query for constructing the relation. Materialized = actually constructed and stored.

Declaring Views Declare by: CREATE [MATERIALIZED] VIEW <name> AS <query>; Default is virtual.

Example: View Definition CanDrink(drinker, beer) is a view “containing” the drinker-beer pairs such that the drinker frequents at least one bar that serves the beer: CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar;

Example: Accessing a View Query a view as if it were a base table. Also: a limited ability to modify views if it makes sense as a modification of one underlying base table. Example query: SELECT beer FROM CanDrink WHERE drinker = ’Sally’;

Interpreting a View Insertion We cannot insert into Synergy --- it is a virtual view. But we can use an INSTEAD OF trigger to turn a (drinker, beer, bar) triple into three insertions of projected pairs, one for each of Likes, Sells, and Frequents. Sells.price will have to be NULL.