Subqueries CIS 4301 Lecture Notes Lecture 17 - 3/23/2006.

Slides:



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

SQL Query Examples Database Management COP4540, SCS, FIU.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
Midterm Review II. Redundancy. –Information may be repeated unnecessarily in several tuples. –E.g. length and filmType. Update anomalies. –We may change.
Relational Operations on Bags Extended Operators of Relational Algebra.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2008.
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
CMSC424: Database Design Instructor: Amol Deshpande
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
CMSC424: Database Design Instructor: Amol Deshpande
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
Relational Operations on Bags Extended Operators of Relational Algebra.
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
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.
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
Chapter 6 The database Language SQL Spring 2011 Instructor: Hassan Khosravi.
Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1.
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
THE DATABASE LANGUAGE SQL
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
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 2015, Fall Pusan National University Ki-Joune Li.
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
SQL Exercises – Part I April
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
The Database Language SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
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 SQL: Concept and Usage. 2 SQL: an Overview SQL (Structured Query Language) –Also be pronounced as “sequel” –A relational database language –Consists.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Slides are reused by the approval of Jeffrey Ullman’s
Chap 5. The DB Language (SQL)
THE DATABASE LANGUAGE SQL
Introduction to Structured Query Language (SQL)
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Basic SQL Lecture 6 Fall
CS 405G: Introduction to Database Systems
IST 210: Organization of Data
SQL: Concept and Usage.
CPSC-310 Database Systems
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More Relation Operations
CSCE 315 – Programming Studio Spring 2010 Project 1, Lecture 4
5.1 Relational Operations on Bags
CS4433 Database Systems SQL - Basics.
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Presentation transcript:

Subqueries CIS 4301 Lecture Notes Lecture /23/2006

Lecture 17© CIS Spring Subqueries So far, conditions in where clause involved scalar values SELECT statements appearing in WHERE clause = “subquery” Treated as value if returns a single tuple result Otherwise treated as relation and compared to values using IN, EXISTS, ALL, ANY

Lecture 17© CIS Spring Subqueries Involving Scalar Values Find the name of the producer of ‘Star Wars’

Lecture 17© CIS Spring Subqueries Involving Scalar Values producerC# Run-time error, if zero or more than one tuple is produced! Different way to look at this: Subquery returning scalar value Unary relation with one tuple Find the name of the producer of ‘Star Wars’ SELECT name FROM MovieExec WHERE cert# = (SELECT DISTINCT producerC# FROM Movie WHERE title = ‘Star Wars’ );

Lecture 17© CIS Spring Conditions Involving Relations Apply set operations to subqueries that produce a relation Will be result of select-from-where query 1. EXISTS R : true iff R not empty Let s be a scalar value, R must be one-column relation 2. s IN R : true if s in R ( can be relaxed to incl. tuple variables ) 3. s op ALL R, op = {,<>,=, …}: true if s greater, smaller, etc. than ALL values in R 4. s op ANY R : true if s greater, smaller, … than at least one value in R

Lecture 17© CIS Spring Set Operators in Subqueries SELECT... WHERE... att < ALL/ANY (subquery)  ALL/ANY > ALL/ANY  ALL/ANY = ALL/ANY <> ALL/ANY Can also precede expression with NOT, e.g.: SELECT... WHERE... NOT (att < ALL/ANY...) scalar value s

Lecture 17© CIS Spring Works for Comparing Tuples SELECT... WHERE... t IN (subquery) If a tuple t has same number of components as R, makes sense to test for set membership using IN operator

Lecture 17© CIS Spring Example Find the producers of Harrison Ford’s movies Option 1: Using Cartesian Product

Lecture 17© CIS Spring Using Nested Subquery Option 2: Using nested subqueries and comparison operator IN SELECT name FROM MovieExec WHERE cert# IN (SELECT producerC# FROM Movie WHERE (title, year) IN (SELECT movieTitle, movieYear FROM StarsIn WHERE starName = ‘Harrison Ford’ ) );

Lecture 17© CIS Spring Correlated Subqueries So far, subqueries have been evaluated ONCE! Correlated subqueries are evaluated many times, once for each assignment to the subquery parameter Find the titles that have been used for two or more movies Step 1: Start with outer query that looks at all tuples in: Movie (title,year,length,inColor,studioName,producerC#) Step 2: For each tuple, we ask subquery whether there is a movie with the same title and a greater year

Lecture 17© CIS Spring Correlated Subqueries Find the titles that have been used for two or more movies SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title ); For each iteration of the outer query, provide value for inner query Scope for tuple variable “Movie” Scope for tuple variable “Old”

Lecture 17© CIS Spring Example Consider MovieStar(Name,address,gender,age) Find all Movie Stars who are younger than the oldest colleague of their gender.

Lecture 17© CIS Spring Another Example Find all producers who made at least one film before 1960 can be rewritten to Find all producers such that there exists at least one film that they produced before 1960

Lecture 17© CIS Spring SQL Join Expressions Can produce new relations using a number of join operators R1 CROSS JOIN R2 behaves just like Cartesian product R1 JOIN R2 ON behaves like a theta join R1 NATURAL JOIN R2 behaves like a natural join Outerjoins

Lecture 17© CIS Spring Examples Movie CROSS JOIN StarsIn; Movies JOIN StarsIn ON title = movieTitle AND year = movieYear; MovieStar NATURAL JOIN MovieExec;

Lecture 17© CIS Spring But Why? Results in more readable queries: SELECT name FROM MovieStar, MovieExec WHERE MovieStar.name = MovieExec.name AND MovieStar.address = MovieExec.address; or SELECT name FROM MovieStar NATURAL JOIN MovieExec; Drawback? Not supported in all dialects of SQL

Lecture 17© CIS Spring Outer Joins In “inner” joins, dangling tuples do NOT show up in the result If we want to see those as well, need new type of join, “outer join” How does it work? Join tuples as before Dangling tuples from either relation will added to the result, padded with NULL values FULL outer join pads dangling tuples from both of the relations LEFT or RIGHT outer join pads dangling tuples from left or right relation only

Lecture 17© CIS Spring Example MovieStar (name,address,gender,bdate) MovieExec(name,address,cert#,networth) MovieStar NATURAL FULL OUTER JOIN MovieExec; Assume we have two movie stars, one of which is also a movie exec, and one movie exec who is not a movie star nameaddressgenderbdatecert#networth Mary Tyler MooreMaple Str.‘F’9/9/ $100M T. HanksCherry Ln.‘M’8/8/88NULL G. LucasOak Rd.NULL 3456$400M

Lecture 17© CIS Spring Duplicate Elimination Be careful about whether of not to expect duplicates in result SQL’s notion of relation different from that used by “pure” relational algebra SQL uses bags or multisets SELECT DISTINCT … However, not all queries suffer from duplicates

Lecture 17© CIS Spring Example SELECT name FROM MovieExec WHERE cert# IN (SELECT producerC# FROM Movie WHERE (title, year) IN (SELECT movieTitle, movieYear FROM StarsIn WHERE starName = ‘Harrison Ford’ ) ); Does it return duplicates or not?

Lecture 17© CIS Spring More on Duplicates UNION, INTERSECTION, EXCEPT automatically remove duplicates If you want to retain them, use keyword ALL

Lecture 17© CIS Spring Aggregation Operations that form a single value from the list of values appearing in the column SUM, AVG, MIN, MAX, COUNT Find the total number of movie executives SELECT COUNT(DISTINCT name) FROM MovieExec; works even if name is not key If we want to be sure not to count same exec twice SELECT COUNT(*) FROM MovieExec; Only allowed with count

Lecture 17© CIS Spring Grouping of Tuples Often, do not want to apply aggregate operator to entire column Find the total number of minutes of movies produced by each studio Want output like: studioSUM (length) Disney MGM Use GROUP BY followed by list of grouping attributes

Lecture 17© CIS Spring GROUP BY Answer SELECT studioName, SUM(length) FROM Movie GROUP BY studioName; Very important!! SELECT clause has two different kinds of terms (1) Aggregations, evaluated on a per-group basis (2) Attributes that appear in the group by clause In a SELECT clause that has aggregations, only those attributes that are mentioned in GROUP BY clause may appear un-aggregated

Lecture 17© CIS Spring Another Group-By Example Find each producer’s total length of film produced

Lecture 17© CIS Spring Restricted Grouping Find total length of movies for producers with net worth greater than $10M.

Lecture 17© CIS Spring Another Example Find total film length for only those producers who made at least one film before SELECT name, SUM(length) FROM Movie, MovieExec WHERE producerC# = cert# AND year < 1930 GROUP BY name; Not correct! Includes only movies made before 1930 in total Need one more clause

Lecture 17© CIS Spring HAVING Mechanism to chose groups based on some aggregate property of the group itself Find total film length for only those producers who made at least one film before 1960 SELECT name,SUM(length) FROM Movie, MovieExec WHERE producerC# = cert# GROUP BY name HAVING MIN(year)< 1960; Removes from grouped relation all those groups in which every tuple had a year component 1960 or higher