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

Slides:



Advertisements
Similar presentations
1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation.
Advertisements

More on Structured Query Language
1 More SQL Database Modification Defining a Database Schema Views.
SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.
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.
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.
Relational Operations on Bags Extended Operators of Relational Algebra.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
CPSC-608 Database Systems Fall 2010 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
Fall 2001Arthur Keller – CS 1806–1 Schedule Today (TH) Bags and SQL Queries. u Read Sections Project Part 2 due. Oct. 16 (T) Duplicates, Aggregation,
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.
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.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
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.
1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
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.
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
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.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
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
Relational Database Systems 1
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
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
CS 440 Database Management Systems
CPSC-608 Database Systems
Database Design and Programming
CPSC-310 Database Systems
IST 210: Organization of Data
CPSC-310 Database Systems
IT 244 Database Management System
CMSC-461 Database Management Systems
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CPSC-608 Database Systems
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and set difference. But: in Oracle set difference is MINUS, not EXCEPT. Example Find the drinkers and beers such that the drinker likes the beer and frequents a bar that serves it. Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar) (SELECT * FROM Likes) INTERSECT (SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar );

Forcing Set/Bag Semantics Default for select-from-where is bag; default for union, intersection, and difference is set. u Why? Saves time of not comparing tuples as we generate them. u But we need to sort anyway when we take intersection or difference. (Union seems to be thrown in for good measure!) Force set semantics with DISTINCT after SELECT. u But make sure the extra time is worth it.

Example Find the different prices charged for beers. Sells(bar, beer, price) SELECT DISTINCT price FROM Sells; Force bag semantics with ALL after UNION, etc.

Join-Based Expressions A number of forms are provided. Can be used either stand-alone (in place of a select-from- where) or to define a relation in the FROM -clause. R NATURAL JOIN S R JOIN S ON condition e.g., condition: R.B=S.B R CROSS JOIN S R OUTER JOIN S Outerjoin can be modified by: 1. Optional NATURAL in front. 2. Optional ON condition at end. 3. Optional LEFT, RIGHT, or FULL (default) before OUTER. LEFT = pad (with NULL ) dangling tuples of R only; RIGHT = pad dangling tuples of S only.

Aggregations Sum, avg, min, max, and count apply to attributes/columns. Also, count(*) applies to tuples. Use these in lists following SELECT. Example Find the average price of Bud. Sells(bar, beer, price) SELECT AVG(price) FROM Sells WHERE beer = 'Bud'; Counts each tuple (presumably each bar that sells Bud) once. Class Problem What would we do if Sells were a bag?

Eliminating Duplicates Before Aggregation Find the number of different prices at which Bud is sold. Sells(bar, beer, price) SELECT COUNT(DISTINCT price) FROM Sells WHERE beer = 'Bud'; DISTINCT may be used in any aggregation, but typically only makes sense with COUNT.

Grouping Follow select-from-where by GROUP BY and a list of attributes. The relation that is the result of the FROM and WHERE clauses is grouped according to the values of these attributes, and aggregations take place only within a group. Example Find the average sales price for each beer. Sells(bar, beer, price) SELECT beer, AVG(price) FROM Sells GROUP BY beer;

Example Find, for each drinker, the average price of Bud at the bars they frequent. Sells(bar, beer, price) Frequents(drinker, bar) SELECT drinker, AVG(price) FROM Frequents, Sells WHERE beer = 'Bud' AND Frequents.bar = Sells.bar GROUP BY drinker; Note: grouping occurs after the and operations.

Restriction on SELECT Lists With Aggregation If any aggregation is used, then each element of a SELECT clause must either be aggregated or appear in a group-by clause. Example The following might seem a tempting way to find the bar that sells Bud the cheapest: Sells(bar, beer, price) SELECT bar, MIN(price) FROM Sells WHERE beer = 'Bud'; But it is illegal in SQL. Problem How would we find that bar?

HAVING Clauses HAVING clauses are selections on groups, just as WHERE clauses are selections on tuples. Condition can use the tuple variables or relations in the FROM and their attributes, just like the WHERE can. u But the tuple variables range only over the group. u And the attribute better make sense within a group; i.e., be one of the grouping attributes.

Example Find the average price of those beers that are either served in at least 3 bars or manufactured by Anheuser-Busch. Beers(name, manf) Sells(bar, beer, price) SELECT beer, AVG(price) FROM Sells GROUP BY beer HAVING COUNT(*) >= 3 OR beer IN ( SELECT name FROM Beers WHERE manf = 'Anheuser-Busch' );

DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of values, associating values with attributes in the order the attributes were declared. u Forget the order? List the attributes as arguments of the relation. Example Likes(drinker, beer) Insert the fact that Sally likes Bud. INSERT INTO Likes(drinker, beer) VALUES('Sally', 'Bud');

Insertion of the Result of a Query INSERT INTO relation (subquery). Example Create a (unary) table of all Sally's potential buddies, i.e., the people who frequent bars that Sally also frequents. Frequents(drinker, bar) CREATE TABLE PotBuddies( name char(30) ); INSERT INTO PotBuddies (SELECT DISTINCT d2.drinker FROM Frequents d1, Frequents d2 WHERE d1.drinker = 'Sally' AND d2.drinker <> 'Sally' AND d1.bar = d2.bar );

Deletion DELETE FROM relation WHERE condition. Deletes all tuples satisfying the condition from the named relation. Example Sally no longer likes Bud. Likes(drinker, beer) DELETE FROM Likes WHERE drinker = 'Sally' AND beer = 'Bud'; Example Make the Likes relation empty. DELETE FROM Likes;

Example Delete all beers for which there is another beer by the same manufacturer. Beers(name, manf) DELETE FROM Beers b WHERE EXISTS (SELECT name FROM Beers WHERE manf = b.manf AND name <> b.name ); Note alias for relation from which deletion occurs.

Semantics is tricky. If A.B. makes Bud and BudLite (only), does deletion of Bud make BudLite not satisfy the condition? SQL semantics: all conditions in modifications must be evaluated by the system before any mods due to that mod command occur. u In Bud/Budlite example, we would first identify both beers a targets, and then delete both.

Updates UPDATE relation SET list of assignments WHERE condition. Example Drinker Fred's phone number is Drinkers(name, addr, phone) UPDATE Drinkers SET phone = ' ' WHERE name = 'Fred'; Example Make $4 the maximum price for beer. Updates many tuples at once. Sells(bar, beer, price) UPDATE Sells SET price = 4.00 WHERE price > 4.00;