Subqueries Example Find the name of the producer of ‘Star Wars’.

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.
SQL This presentation will cover: A Brief History of DBMS View in database MySQL installation.
SQL reviews. Stored Procedures Create Procedure Triggers.
SQL CSET 3300.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
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 Algebra.
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.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Indexes. An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a fixed value for attribute.
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.
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
CMSC424: Database Design Instructor: Amol Deshpande
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.
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.
Database Modifications A modification command does not return a result as a query does, but it changes the database in some way. There are three kinds.
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.
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.
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
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.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
Introduction to Indexes. Indexes An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a.
THE DATABASE LANGUAGE SQL
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
© 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.
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 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
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
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
The Database Language SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
Slides are reused by the approval of Jeffrey Ullman’s
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Chap 5. The DB Language (SQL)
Chapter 3 Introduction to SQL(3)
Databases : More about SQL
CPSC-310 Database Systems
Introduction to Structured Query Language (SQL)
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Database Design and Programming
CS 405G: Introduction to Database Systems
IST 210: Organization of Data
CPSC-310 Database Systems
More Relation Operations
SQL This presentation will cover: View in database MySQL installation
CS4433 Database Systems SQL - Basics.
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Presentation transcript:

Subqueries Example Find the name of the producer of ‘Star Wars’. Movie(title, year, length, inColor, studioName, producerC) MovieExec(name, address, cert, netWorth) We can do: SELECT name FROM Movie, MovieExec WHERE title ='Star Wars' AND producerC =cert; Or we can have a subquery: FROM MovieExec WHERE cert =(SELECT producerC FROM Movie WHERE title = 'Star Wars'); If we can deduce that there will be only a single value produced by the subquery, then we can use this expression, surrounded by parentheses, as if it were a constant.

Conditions Involving Relations There are a number of SQL operators that we can apply to a relation R and produce a Boolean result. Typically used in the WHERE clause. 1. EXISTS R is a condition that is true if R is not empty. 2. s IN R is true if s is equal to one of the tuples in R. Likewise, s NOT IN R is true if and only if s is equal to no tuple in R.

Example Give all the producers of movies in which Julia Roberts stars. SELECT name FROM MovieExec WHERE cert IN (SELECT producerC FROM Movie WHERE (title, year) IN (SELECT movieTitle, movieYear FROM StarsIn WHERE starName = 'Julia Roberts'));

Remark The previous nested query can, like many nested queries, be written as a single SELECT-FROM-WHERE expression. SELECT name FROM MovieExec, Movie, StarsIn WHERE cert = producerC AND title = movieTitle AND year = movie Year AND starName = 'Julia Roberts';

(Continued) 3. s > ALL R is true if s is greater than every value in the unary (one column) relation R. Similarly, the > operator could be replaced by any other comparison operators with the analogous meaning. For instance, s <> ALL R is the same as s NOT IN R. 4. s > ANY R is true if s is greater than at least one value in unary relation R. Similarly we can use any other comparison operators in place of >. For instance, s =ANY R is the same as s IN R. EXISTS, ALL, and ANY operators can be negated by putting NOT in front of the entire expression.

Bag Semantics and Union, Intersection and Difference Although the SELECT-FROM-WHERE statement uses bag semantics, the default for union, intersection, and difference is set semantics. Motivation? When doing projection in relational algebra, it is easier to avoid eliminating duplicates. Just work tuple-at-a-time. When doing 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 . . . Only UNION ALL supported in ORACLE. Example Find all the different studios producing movies: Movie(title, year, length, inColor, studioName, producerC), SELECT DISTINCT studioname FROM Movie; Notice that without DISTINCT, a studioname would be listed as many times as there were movies from that studio.

Aggregations SUM, AVG, COUNT, MIN, and MAX can be applied to a column in a SELECT clause to produce that aggregation on the column. Example Find the average length of movies from Disney. SELECT AVG(length) FROM Movie WHERE studioName = 'Disney'; Remark We can also use COUNT(*) which counts the number of tuples in the relation constructed from the FROM and WHERE clauses of the query.

Eliminating Duplicates in an Aggregation DISTINCT inside an aggregation causes duplicates to be eliminated before the aggregation. Example Find the number of different producers for Disney movies. SELECT COUNT(DISTINCT producerc) FROM Movie WHERE studioname = 'Disney'; This is not the same as: SELECT DISTINCT COUNT(producerc) DISTINCT here is useless! Why?

Not only in COUNT… SELECT AVG(DISTINCT length) FROM Movie WHERE studioname = 'Disney'; This will produce the average of only the distinct values for length.

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. SELECT SUM(networth) FROM moviestar NATURAL FULL OUTER JOIN movieexec; But if there are no non-NULL values in a column, then the result of the aggregation is NULL.

Example: Effect of NULL’s SELECT count(*) FROM Movie WHERE studioName = 'Disney'; SELECT count(length) The number of movies from Disney. The number of movies from Disney with a known length.

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 the listed attributes in GROUP BY, and any aggregation is applied only within each group. Example From the Movie relation, find the average length for each studio. SELECT studioName, AVG(length) FROM Movie GROUP BY studioName;

Another Example From Movie and MovieExec, find the producer’s total length of film produced: SELECT name, SUM(length) FROM Movie, MovieExec WHERE producerc = cert GROUP BY name; Compute those tuples first, then group by name.

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.

Illegal Query Example SELECT title, length We might think we could find the shortest movie of Disney as: SELECT title, MIN(length) FROM Movie WHERE studioName = 'Disney'; But this query is illegal in SQL. Because title is neither aggregated nor on the GROUP BY list. We should do instead: SELECT title, length WHERE studioName = 'Disney' AND length = (SELECT MIN(length) WHERE studioName = 'Disney');

Or… …resembling Relational Algebra. SELECT title, length FROM Movie NATURAL JOIN (SELECT MIN(length) AS length FROM Movie WHERE studioName = 'Disney') WHERE studioName = 'Disney'; …resembling Relational Algebra.

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 Consider again the query SELECT name, SUM(length) FROM Movie, MovieExec WHERE producerc = cert GROUP BY name; Suppose we didn’t wish to include all the producers in our table of aggregated lengths. We want those producers with networth less than 1,000,000, and that have at least one movie before 1973. Solution FROM MovieExec, Movie WHERE producerc = cert AND networth<1000000 GROUP BY name HAVING MIN(year) < 1973;

Requirements on HAVING Conditions These conditions may refer to any relation in the FROM clause. They may refer to attributes of those relations, as long as the attribute makes sense within a group; i.e., it is either: A grouping attribute, or Aggregated attribute.

“Having” is a special kind of  The previous query can also be written as: SELECT name, sumLength FROM ( SELECT name, MIN(year) AS minYear, SUM(length) AS sumLength FROM MovieExec, Movie WHERE producerc = cert AND networth < 1000000 GROUP BY name) WHERE minYear < 1973;

Correlated Subqueries Suppose StarsIn relation has an additional attribute “salary” StarsIn(movieTitle, movieYear, starName, salary) Now, find the stars who were paid for some movie more than the average salary for that movie. SELECT starName, movieTitle, movieYear FROM StarsIn X WHERE salary > (SELECT AVG(salary) FROM StarsIn WHERE movieTitle = X.movieTitle AND movieYear=X.movieYear); Remarks Outer query cannot reference any columns in the subquery. Subquery references the tuple in the outer query. Value of the tuple changes by row of the outer query, so the database must rerun the subquery for each row comparison.

Another Solution (Nesting in FROM) SELECT X.starName, X.movieTitle, X.movieYear FROM StarsIn X, (SELECT movieTitle, movieYear, AVG(salary) AS avgSalary FROM StarsIn GROUP BY movieTitle, movieYear) Y WHERE X.salary>Y.avgSalary AND X.movieTitle=Y.movieTitle AND X.movieYear=Y.movieYear;

Exercise Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) Find those manufacturers that sell Laptops, but not PC's. Find those hard-disk sizes that occur in two or more PC's. Find those manufacturers of at least two different computers (PC or Laptops) with speed of at least 700. Find the manufacturers who sell exactly three different models of PC.