Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations.

Similar presentations


Presentation on theme: "Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations."— Presentation transcript:

1 Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations

2 Miscellany Lab and Homework #5 Questions? Go over some problems? Good News More bonus questions to come

3 Topics for Today Set Operations UNION (Pages 140 – 144)‏ UNION ALL (Pages 144 – 145)‏ Subqueries... WHERE clause (Pages 58 – 64)‏ HAVING clause (not in book)‏ FROM clause (Pages 86 – 88)‏ SELECT clause (Pages 151 – 152)‏ Correlated Subqueries (Pages 153 – 158)‏

4 Set Operations UNION (supported by MySQL)‏ UNION ALL (supported by MySQL)‏ INTERSECT (not supported by MySQL)‏ EXCEPT/MINUS (not supported by MySQL)‏

5 UNION Combines the results from multiple SELECT statements into a single result set

6 UNION Syntax Two tables: SELECT... UNION [DISTINCT | ALL] SELECT... Three or more tables: SELECT... UNION [DISTINCT | ALL] SELECT... UNION [DISTINCT | ALL] SELECT......

7 UNION Restrictions Each select clause must contain the same number of columns This is an error!!!! SELECT ArtistName AS ArtistStudioNames, Region FROM Artists UNION SELECT StudioName FROM Studios ORDER BY ArtistStudioNames;

8 UNION Example UNION can also be used to append aggregate information, similar to WITH ROLLUP: -- For each gender, list the number of members of that gender and append a total member count to the bottom of the list. SELECT Gender, COUNT(Gender) FROM Members GROUP BY Gender UNION SELECT 'Total', COUNT(Gender) FROM Members;

9 UNION and Duplicates By default, the UNION keyword alone implicitly removes duplicates To remove duplicates explicitly, use: UNION DISTINCT To keep duplicates use: UNION ALL Example: SELECT Region FROM Artists UNION ALL SELECT Region FROM Studios;

10 UNION and Sorting To order all results in a UNION query, wrap the SELECT queries in parentheses, and at the end, use an ORDER BY clause that orders on a column alias defined in the first UNION Example: (SELECT ArtistName AS ArtistStudioNames FROM Artists) UNION (SELECT StudioName FROM Studios) ORDER BY ArtistStudioNames;

11 Subqueries Subqueries are queries within queries. Another multi-table selection technique Example: List all artists that do not have a title. SELECT ArtistName FROM Artists WHERE ArtistID NOT IN (SELECT ArtistID FROM Titles);

12 WHERE Clause Subqueries Use a subquery in the WHERE clause when you want to filter records from the outer query using a single value or list of values returned from one or more subqueries Always wrap your subquery in parentheses When filtering with single values, always think substitution When filtering with a list of values (a column of data), use the keywords IN, ALL, and ANY.

13 WHERE Clause Subquery Example List all members whose sales contact is Bob Bentley or Lisa Williams The outer query... SELECT FirstName, LastName FROM Members WHERE SalesID = (???) OR SalesID = (???); The inner queries... SELECT SalesID FROM SalesPeople WHERE FirstName = 'Bob' AND LastName = 'Bentley'; SELECT SalesID FROM SalesPeople WHERE FirstName = 'Lisa' AND LastName = 'Williams';

14 WHERE Clause Subquery Example Now substitute the subqueries back into the outer query, surrounding them with parentheses SELECT FirstName, LastName FROM Members WHERE SalesID = (SELECT SalesID FROM SalesPeople WHERE FirstName = 'Bob' AND LastName = 'Bentley') OR SalesID = (SELECT SalesID FROM SalesPeople WHERE FirstName = 'Lisa' AND LastName = 'Williams');

15 WHERE Clause Subquery Example Did I say you can't have aggregates in the WHERE clause? Not totally true. You can, but you need to put the aggregate calculation in a subquery so that it is computed first! -- List all tracks that are longer than the average track length. SELECT TrackTitle FROM Tracks WHERE LengthSeconds > (SELECT AVG(LengthSeconds) FROM Tracks);

16 IN and NOT IN Use the IN keyword to test if the value of a field matches any items in a list (typically returned by a subquery)‏ Syntax: attribute_name IN (subquery)‏ attribute_name NOT IN (subquery)‏

17 IN Example Example: List the names of all artists who have authored titles. The outer query... SELECT ArtistName FROM Artists WHERE ArtistID IN (???); The inner query... SELECT ArtistID FROM Titles; Substitute to get the solution... SELECT ArtistName FROM Artists WHERE ArtistID IN (SELECT ArtistID FROM Titles);

18 ALL and ANY ALL The condition must hold true for all elements in the list. Syntax: attribute_name operator ALL (subquery)‏ ANY The condition may hold true for only one or more elements in the list. Syntax: attribute_name operator ANY (subquery)‏

19 ALL and ANY Examples List all artists whose entry dates are older than all of the artists from New York and California. SELECT ArtistName FROM Artists WHERE EntryDate < ALL (SELECT EntryDate FROM Artists WHERE Region = 'NY' OR Region = 'CA'); SELECT ArtistName FROM Artists WHERE (EntryDate < ALL (SELECT EntryDate FROM Artists WHERE Region = 'NY')) AND (EntryDate < ALL (SELECT EntryDate FROM Artists WHERE Region = 'CA'));

20 HAVING Clause Subqueries Like the WHERE clause, you can have subqueries in the HAVING clause as well Think substitution as well -- List each city and region for which the number of members in each city and region outnumber or equal the total number of studios (3). SELECT City, Region, COUNT(*) AS C FROM Members GROUP BY City, Region HAVING C >= (SELECT COUNT(*) FROM Studios);

21 FROM Clause Subqueries Queries oftentimes return tables FROM clause arguments are tables Therefore, you can have (sub)queries in the FROM clause Always wrap your subqueries in parentheses Always define a table alias for any table returned by a subquery in the FROM clause

22 FROM Clause Subquery Usage Use a subquery in the FROM clause when you want to do a join between tables A and B, and it is possible to filter records from tables A and B before you do the join What this does is save SQL from having to compute massive Cartesian products Let's do an example: For each artist that has a title, list all artist names and titles where the genre is alternative.

23 FROM Clause Subquery Example Example (without subquery): SELECT ArtistName, Title, Genre FROM Artists JOIN Titles USING(ArtistID) WHERE Genre = 'alternative'; Example (with subquery): SELECT A.ArtistName, T.Title, T.Genre FROM Artists A JOIN (SELECT * FROM Titles WHERE Genre = 'alternative') T USING(ArtistID);

24 FROM Clause Subquery Example Was last example really necessary? Most modern DBMSs should have good join optimizers Let's try a more complex example where a FROM clause subquery is required: -- For only those genres that have titles, list each genre along with the average artist name length for that genre. Exclude duplicate artist names from the aggregate (average) calculation. Order by the average artist name length in descending order. Remember to think substitution!

25 FROM Clause Subquery Solution Let's try this: Let's call this table X: (SELECT DISTINCT Genre, ArtistName FROM Genre JOIN Titles USING(Genre) JOIN Artists USING(ArtistID)) X Now let's aggregate on X: SELECT X.Genre, AVG(CHAR_LENGTH(X.ArtistName)) AS AverageArtistNameLength FROM X GROUP BY X.Genre ORDER BY AverageArtistNameLength DESC;

26 FROM Clause Subquery Solution Now substitute to get the final solution: SELECT X.Genre, AVG(CHAR_LENGTH(X.ArtistName)) AS AverageArtistNameLength FROM (SELECT DISTINCT Genre, ArtistName FROM Genre JOIN Titles USING(Genre) JOIN Artists USING(ArtistID)) X GROUP BY X.Genre ORDER BY AverageArtistNameLength DESC;

27 SELECT Clause Subqueries Use a subquery in the SELECT clause when you want to display a column of data based on data from another table or aggregrate calculations Any subquery in the SELECT clause must return a single value (you cannot return a list of values or a table)‏ Useful when main query and subquery are correlated (the subquery uses a value or values from the main query)‏

28 SELECT Clause Example For each title, list the artist name, the title name, all tracks associated with each title, and the percent playing time of each track. OUCH!!! Divide the problem into two steps. -- First step: Display all artist and track information. SELECT ArtistName, TitleID, Title, TrackTitle, LengthSeconds FROM Artists JOIN Titles USING(ArtistID) JOIN Tracks USING(TitleID) ORDER BY ArtistID, TitleID, TrackNum;

29 SELECT Clause Example -- Second Step: Display aggregate information. SELECT TitleID, SUM(LengthSeconds) FROM Tracks GROUP BY TitleID; -- Third Step: We need only one aggregate value. SELECT SUM(LengthSeconds) FROM Tracks GROUP BY TitleID HAVING TitleID = ???;

30 SELECT Clause Example -- Fourth Step: Correlate! SELECT ArtistName, K.TitleID, Title, TrackTitle, LengthSeconds FROM Artists JOIN Titles K USING(ArtistID) JOIN Tracks USING(TitleID) ORDER BY ArtistID, TitleID, TrackNum; -- and SELECT SUM(LengthSeconds) FROM Tracks GROUP BY TitleID HAVING TitleID = K.TitleID;

31 SELECT Clause Example -- Fifth step: Put it all together. SELECT ArtistName, K.TitleID, Title, TrackTitle, LengthSeconds, (SELECT SUM(LengthSeconds) FROM Tracks GROUP BY TitleID HAVING TitleID = K.TitleID) AS 'Total Length' FROM Artists JOIN Titles K USING(ArtistID) JOIN Tracks USING(TitleID) ORDER BY ArtistID, TitleID, TrackNum;

32 SELECT Clause Example -- Sixth step: Do the math. SELECT ArtistName, K.TitleID, Title, TrackTitle, LengthSeconds, 100*LengthSeconds/(SELECT SUM(LengthSeconds) FROM Tracks GROUP BY TitleID HAVING TitleID = K.TitleID) AS '% Length' FROM Artists JOIN Titles K USING(ArtistID) JOIN Tracks USING(TitleID) ORDER BY ArtistID, TitleID, TrackNum;


Download ppt "Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations."

Similar presentations


Ads by Google