CPSC-310 Database Systems

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.
IS698: Database Management Min Song IS NJIT. Overview  Query processing  Query Optmization  SQL.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
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 #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.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
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
Computational Biology Dr. Jens Allmer Lecture Slides Week 6.
Databases 1 Second lecture.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
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 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
CPSC-310 Database Systems
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
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
Database Models Relational Model
CPSC-310 Database Systems
IST 210: Organization of Data
IT 244 Database Management System
2018, Fall Pusan National University Ki-Joune Li
CMSC-461 Database Management Systems
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:

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

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.

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)

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

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.

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.

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.

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

The IN Operator

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

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.

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

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.

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

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

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

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

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

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.

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

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

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

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

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

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

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 Operators ANY and ALL

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

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.

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.

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.

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.

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.

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

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

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

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

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

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)

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.

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)

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.

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.

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

Bag Semantics

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

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?

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

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.

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.

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.

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

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:

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

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.

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.

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

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 Cross join (Cartesian Product): R CROSS JOIN S;

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

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

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 4 7 8 R S A B C D 1 2 5 6 3 4 7 8

Theta Join R JOIN S ON <condition>

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 4 7 2 R S A R.B S.B C D 2 2 5 6 1 2 4 7 2 4 5 2 5 6 ON A < D;

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 4 7 2 R S A R.B S.B C D 2 2 5 6 1 2 4 7 2 4 5 2 5 6 ON A < D;

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.

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

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

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

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