Download presentation
Presentation is loading. Please wait.
1
CS122 Using Relational Databases and SQL
5/9/2018 CS122 Using Relational Databases and SQL 7. Advanced queries Huiping Guo Department of Computer Science California State University, Los Angeles
2
Outline Queries with UNION Sub-Queries in the FROM clause
Sub-Queries in the SELECT clause Correlated Sub-Queries EXISTS with Sub-Queries Nested Sub-Queries 7. Advanced subqueries CS122_W16
3
Joins vs. Unions Table 1 Table 1 Table 2 Table 2 Union Join
7. Advanced subqueries CS122_W16
4
Queries UNION Combine data from different tables
ORDER BY clause in a UNION UNION ALL Using UNION to report both Detail and Total Using UNION to report multiple statistics 7. Advanced subqueries CS122_W16
5
Combine data from different tables
Lyric Music wants to send Christmas cards to all members and all studios. Produce a Christmas card list containing the name and postal address of all members and studios SELECT firstName, lastName, address, city, region, postalCode FROM Members UNION SELECT studioName, ' ', address, city, region, postalCode FROM Studios 7. Advanced subqueries CS122_W16
6
Union Rules Must have same number of fields
Use dummy fields if needed Must have same data types in corresponding fields Corresponding fields do not need to have the same names Field names from first query will be used in results Each query of the union is independent 7. Advanced subqueries CS122_W16
7
Another example List all studios and members who are the responsible
party for an artist, identifying the source as S or M SELECT 'M' As Src, firstName, lastName, address, city, region, postalCode FROM Members M INNER JOIN XrefArtistsMembers X ON M.MemberID = X.MemberID WHERE respParty=1 UNION SELECT 'S', studioName, ' ', address, city,region, postalCode FROM Studios; 7. Advanced subqueries CS122_W16
8
ORDER BY clause in a UNION
A UNION can have only one ORDER BY clause It must be placed at the end of the query It must refer to column names in the first SELECT clause 7. Advanced subqueries CS122_W16
9
Example Is this correct? SELECT 'M', firstName, lastName FROM Members
ORDER BY firstName UNION ALL SELECT 'S', studioName, ' ' FROM Studios ORDER BY studioName; 7. Advanced subqueries CS122_W16
10
Example The correct one: SELECT 'M', firstName, lastName FROM Members
UNION SELECT 'S', studioName, ' ' FROM Studios ORDER BY firstName 7. Advanced subqueries CS122_W16
11
Union vs. Union All UNION eliminate duplicates
UNION ALL keeps duplicates Example SELECT region FROM Artists UNION SELECT region FROM Studios ORDER BY region 7. Advanced subqueries CS122_W16
12
Using Union to Report Both Detail and Total
Union can also report the data from a single table massaged in different ways Use Union to join the detail from a table with the aggregate from the same table to report both detail and totals together Ex. Report all tracks and the total time of these tracks 7. Advanced subqueries CS122_W16
13
Using Union to Report Both Detail and Total
Select TrackNum, TrackTitle, LengthSeconds From Tracks Union Select 99, 'Total', Sum(LengthSeconds) From Tracks Order by TrackNum TrackNum TrackTitle LengthSeconds Bob's Dream My Wizard Third's Folly Leather Hot Cars Cool Nights Music in You Don't Care About Time Kiss Pizza Box Goodbye Total 99 selected to fill column and allow proper sorting 7. Advanced subqueries CS122_W16
14
Using Union to Report Multiple Summary Statistics
Use Union to report multiple independent aggregates of some data and report all the results together Ex. Report a count of tracks that are less than two minutes, between two and three minutes, and more than three minutes. 7. Advanced subqueries CS122_W16
15
Using Union to Report Multiple Summary Statistics
Select 1, '< 2 minutes' As Length, Count(*) As NumTracks From Tracks Where LengthSeconds<120 Union Select 2, '2-3 minutes', Count(*) From Tracks Where LengthSeconds Between 120 and 180 Select 3, '>3 minutes', Count(*) From Tracks Where LengthSeconds>180 Length NumTracks < 2 minutes 21 minutes 22 >3 minutes 7 7. Advanced subqueries CS122_W16
16
Cautions on Union Overuse
Overuse can indicate poorly-designed data Unions generally run much slower than Joins For Joins the database can generally take advantage of indices on primary and foreign keys But a Union (especially without All option) has to compare all the columns from every SELECT 7. Advanced subqueries CS122_W16
17
Outline Queries with UNION Sub-Queries in the FROM clause
Sub-Queries in the SELECT clause Correlated Sub-Queries EXISTS with Sub-Queries Nested Sub-Queries 7. Advanced subqueries CS122_W16
18
Sub-Queries in FROM Syntax: Select fields
From Tabel1 Inner|Left|Right JOIN (Select fields From Table2) Alias ON Table1.field=Alias.field 7. Advanced subqueries CS122_W16
19
Sub-Queries in FROM List all artists with members in Georgia.
Select Distinct Artistname From (Artists A Inner Join XrefArtistsMembers X On A.ArtistID = X.ArtistID) Inner Join (Select MemberID From Members M Where M.Region='GA') M On M.MemberID = X.MemberID 7. Advanced subqueries CS122_W16
20
Sub-Queries in FROM (Cont.)
5/9/2018 Sub-Queries in FROM (Cont.) Another way: Select Distinct Artistname From Artists A Inner Join (Select ArtistID From Members M Inner Join XrefArtistsMembers X On M.MemberID = X.MemberID Where M.Region='GA') SC On A.ArtistID = SC.ArtistID 7. Advanced subqueries CS122_W16
21
Outline Queries with UNION Sub-Queries in the FROM clause
Sub-Queries in the SELECT clause Correlated Sub-Queries EXISTS with Sub-Queries Nested Sub-Queries 7. Advanced subqueries CS122_W16
22
Sub-Queries in Select Clause (non-correlated)
Syntax SELECT Field, Field, (Select Field From Table2) FROM Table1 The sub-query MUST return Only one row and one column The sub-query can run independently 7. Advanced subqueries CS122_W16
23
Sub-Queries in Select Clause (non-correlated)
List track title, length in seconds, and the total seconds for the entire CD for all tracks in TitleID 4 Select titleid, TrackTitle, LengthSeconds From Tracks Where titleid=4 union Select 4, 'Total', Sum(lengthseconds) From Tracks where titleid=4 How to make the total time appear in each row? 7. Advanced subqueries CS122_W16
24
Sub-Queries in Select Clause (non-correlated)
Use a subquery in Select clause! Select titleid, TrackTitle, lengthSeconds, (Select Sum(lengthseconds) From Tracks where titleid=4) As TotSec From Tracks Where titleid=4 7. Advanced subqueries CS122_W16
25
Sub-Queries in Select Clause (non-correlated)
List track title, length in seconds, and the total seconds and the length as a percentage of the total time for the CD for all tracks in TitleID 4 Select titleid, TrackTitle, LengthSeconds, (Select Sum(lengthseconds) From Tracks where titleid=4) As TotSec, LengthSeconds /(Select Sum(lengthseconds) From Tracks where titleid=4) *100 As Percentage From Tracks Where titleid=4 7. Advanced subqueries CS122_W16
26
Outline Queries with UNION Sub-Queries in the FROM clause
Sub-Queries in the SELECT clause Correlated Sub-Queries EXISTS with Sub-Queries Nested Sub-Queries 7. Advanced subqueries CS122_W16
27
Correlated Sub-Queries
Syntax SELECT Field, Field, (Select Field From Table2 Where table2.Field=Table1.Field) FROM Table1 WHERE clause in sub-query joins each row to appropriate row of the outer query 7. Advanced subqueries CS122_W16
28
Example List track title, length in seconds, and the total seconds for the entire CD for all tracks in TitleID 4 in each titleID SELECT titleid,trackTitle, lengthSeconds As Sec, ( Select Sum(lengthseconds) From Tracks SC Where SC.titleID=T.titleID) As TotSec FROM Tracks T 7. Advanced subqueries CS122_W16
29
Outer Table: Tracks (T)
TitleID TrackTitle Length 1 Song 1 10 Song 2 30 2 Song 3 20 Song 4 Song 5 40 Sub-query Select Sum(lengthseconds) From Tracks SC Where SC.titleID=T.TitleID Query results 1 TitleID TrackTitle Length TotalSec 1 Song 1 10 40 7. Advanced subqueries CS122_W16
30
Outer Table: Tracks (T)
TitleID TrackTitle Length 1 Song 1 10 Song 2 30 2 Song 3 20 Song 4 Song 5 40 Sub-query Select Sum(lengthseconds) From Tracks SC Where SC.titleID=T.TitleID Query results 1 TitleID TrackTitle Length TotalSec 1 Song 1 10 40 Song 2 30 7. Advanced subqueries CS122_W16
31
Outer Table: Tracks (T)
TitleID TrackTitle Length 1 Song 1 10 Song 2 30 2 Song 3 20 Song 4 Song 5 40 Sub-query Select Sum(lengthseconds) From Tracks SC Where SC.titleID=T.TitelID Query results 2 TitleID TrackTitle Length TotalSec 1 Song 1 10 40 Song 2 30 2 Song 3 20 90 7. Advanced subqueries CS122_W16
32
Outer Table: Tracks (T)
TitleID TrackTitle Length 1 Song 1 10 Song 2 30 2 Song 3 20 Song 4 Song 5 40 Sub-query Select Sum(lengthseconds) From Tracks SC Where SC.titleID=T.TitelID Query results 2 TitleID TrackTitle Length TotalSec 1 Song 1 10 40 Song 2 30 2 Song 3 20 90 7. Advanced subqueries CS122_W16
33
Outer Table: Tracks (T)
TitleID TrackTitle Length 1 Song 1 10 Song 2 30 2 Song 3 20 Song 4 Song 5 40 Sub-query Select Sum(lengthseconds) From Tracks SC Where SC.titleID=T.TitelID Query results 2 TitleID TrackTitle Length TotalSec 1 Song 1 10 40 Song 2 30 2 Song 3 20 90 Song 5 7. Advanced subqueries CS122_W16
34
Correlated Sub-Queries
Sub-query returns multiple rows without a GROUP BY and a non-aggregate in SELECT Refers in the sub-query to T table alias that exists only in the outer query Because of unique syntax the sub-query cannot be debugged as independent query Select TrackTitle, LengthSeconds As Sec, (Select Sum(lengthseconds) From Tracks SC Where SC.TitleID=T.TitleID) As TotSec 7. Advanced subqueries CS122_W16
35
The same example Use From clause subquery to answer the same question:
SELECT t.titleid,tracktitle, lengthseconds, totsec FROM Tracks t inner join (select titleid, sum(lengthseconds) as totsec from Tracks group by titleid) sc on sc.titleid=t.titleid 7. Advanced subqueries CS122_W16
36
Outline Queries with UNION Sub-Queries in the FROM clause
Sub-Queries in the SELECT clause Correlated Sub-Queries EXISTS with Sub-Queries Nested Sub-Queries 7. Advanced subqueries CS122_W16
37
EXISTS with Sub-Queries
Syntax SELECT Field1, Field2 FROM Table1 Where Exists (Select Field1, Field2 from Table2 Where Table1.Field1=Table2.Field1) Sub-query returns one or more rows which are correlated with rows of outer query If any matching sub-query rows are found, row from outer query is reported 7. Advanced subqueries CS122_W16
38
EXISTS with Sub-Queries
EXISTS checks for the existence of data in the sub-query Data is either there (True) or it isn't (False) Often used as a WHERE clause sub-query but with correlated sub-query syntax Example: Report a list of all artists with more than one member 7. Advanced subqueries CS122_W16
39
EXISTS with Sub-Queries
Select Artistname From Artists A Where Exists (Select ArtistID, Count(MemberID) From XrefArtistsMembers X Where X.ArtistID= A.ArtistID Group by ArtistID Having Count(MemberID)>1) Artistname Word Sonata The Bullets Confused The Kicks Today 21 West Elm Highlander ArtistID Sub-query results 7. Advanced subqueries CS122_W16
40
Correlated Sub-Queries vs. Joins
Correlated sub-query Select Artistname From Artists A Where Exists (Select ArtistID, Count(MemberID) From XrefArtistsMembers X Where X.ArtistID= A.ArtistID Group by ArtistID Having Count(MemberID)>1) Select Artistname From Artists A Inner Join (Select ArtistID, Count(MemberID) as NumMem From XrefArtistsMembers Group by ArtistID Having Count(MemberID)>1) SQ On A.ArtistID = SQ.ArtistID Join with same results 7. Advanced subqueries CS122_W16
41
Outline Queries with UNION Sub-Queries in the FROM clause
Sub-Queries in the SELECT clause Correlated Sub-Queries EXISTS with Sub-Queries Nested Sub-Queries 7. Advanced subqueries CS122_W16
42
Nested Sub-queries SQL allows nesting sub-queries within sub-queries
Number of levels allowed is not so important You will get confused before SQL does Example List the names of all the artists who have recorded a title with ten or more tracks 7. Advanced subqueries CS122_W16
43
Example Select Artistname From Artists A Where Exists
(Select ArtistID From Titles T Where T.ArtistID= A.ArtistID And Exists (Select TitleID, Count(TrackNum) From Tracks TR Where TR.TitleID = T.TitleID Group by TitleID Having Count(Tracknum)>=10) ) 7. Advanced subqueries CS122_W16
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.