Download presentation
Presentation is loading. Please wait.
1
CS122 Using Relational Databases and SQL
10/29/2019 CS122 Using Relational Databases and SQL 4. Subqueries and joins Daniel Firpo Slides prepared by Randy Moss Department of Computer Science California State University, Los Angeles
2
Outline Subqueries Joining tables Subqueries in the WHERE clause
10/29/2019 Outline Subqueries Subqueries in the WHERE clause Subqueries with IN, ALL & ANY Joining tables Learn how SQL joins tables Join tables using an Equi Join Join tables using an Inner Join 4. Subqueries and joins CS1222-S2019
3
Sub-Queries A query within a query Sub-query placed in parentheses
10/29/2019 Sub-Queries A query within a query Sub-query placed in parentheses Sub-queries can be placed in WHERE clause HAVING clause SELECT clause FROM clause 4. Subqueries and joins CS1222-S2019
4
Sub-Queries examples (1)
10/29/2019 Sub-Queries examples (1) List the name of the oldest member Find the earliest birthday ( ) Select min(birthday) From Members Find the member whose birthday is ‘ ’ Select firstname, lastname Where birthday = ‘ ’ 4. Subqueries and joins CS1222-S2019
5
Sub-Queries examples (1)
10/29/2019 Sub-Queries examples (1) Combine the two queries SELECT firstname, lastname FROM Members WHERE birthday = (SELECT Min(birthday) FROM Members ) 4. Subqueries and joins CS1222-S2019
6
Outer query looks for records with Birthday matching Sub-Query value
Sub-Query Example (1) Outer query looks for records with Birthday matching Sub-Query value Select Lastname, Firstname From Members Where Birthday = (Select Min(Birthday) From Members) Sub-Query returns Query returns Lastname Firstname Wong Tony
7
Sub-Queries examples (2)
10/29/2019 Sub-Queries examples (2) List all track titles and lengths of all tracks whose length is longer than the average of all track lengths Find the average track lenghths (276.08) SELECT Avg(lengthseconds) FROM Tracks Find the all track titles and lengths of all tracks whose length is greater than SELECT tracktitle, lengthseconds FROM Tracks WHERE lengthseconds > 4. Subqueries and joins CS1222-S2019
8
Sub-Queries examples (2)
10/29/2019 Sub-Queries examples (2) Combine the two queries SELECT tracktitle, lengthseconds FROM Tracks WHERE lengthseconds > ( SELECT Avg(lengthseconds) FROM Tracks ) 4. Subqueries and joins CS1222-S2019
9
10/29/2019 Sub-Queries using IN Report the name of all artists who have recorded a title SELECT artistName FROM Artists WHERE artistID IN (SELECT artistID FROM Titles) 4. Subqueries and joins CS1222-S2019
10
ALL & ANY Used with Sub-Queries
10/29/2019 ALL & ANY Used with Sub-Queries Can be used with greater than and less than tests If using ANY, then comparison will be true if it satisfies any value produced by the sub-query SOME is functionally equivalent to ANY If using ALL, then comparison will be true if it satisfies all values produced by the sub-query. 4. Subqueries and joins CS1222-S2019
11
ALL example 10/29/2019 List the name, region, and birthday of every member who is older than all of the members in Georgia Select Birthday From Members Where Region='GA' Birthday Select Lastname, Firstname, Region, Birthday From Members Where Birthday < ALL(Select Birthday From Members Where Region='GA') Lastname Firstname Region Birthday Ranier Brian ONT Kale Caroline VA Wong Tony ONT Cleaver Vic VT 4. Subqueries and joins CS1222-S2019
12
ANY example WHERE region = 'GA' )
10/29/2019 ANY example List the name, region, and birthday of every member who is older than any of the members in Georgia SELECT lastname, firstname, region, birthday FROM Members WHERE birthday < ANY (SELECT birthday FROM Members WHERE region = 'GA' ) 4. Subqueries and joins CS1222-S2019
13
10/29/2019 Exercises Report the firstname, lastname and birthday of the members who has the same birthday as someone in Georgia Is the following query correct? SELECT lastname, firstname, region, birthday FROM Members WHERE birthday = ALL (SELECT birthday FROM Members WHERE region = 'GA' ) No! We cannot compare a single value with a LIST of values 4. Subqueries and joins CS1222-S2019
14
Exercises (cont.) How about this query?
10/29/2019 Exercises (cont.) How about this query? SELECT lastname, firstname, region, birthday FROM Members WHERE birthday = (SELECT birthday FROM Members WHERE region = 'GA') No! We cannot compare a single value with a LIST of values We cannot compre 3 with (3,4,5,6) 4. Subqueries and joins CS1222-S2019
15
Exercises (cont.) This one?
10/29/2019 Exercises (cont.) This one? SELECT lastname, firstname, region, birthday FROM Members WHERE birthday = ANY (SELECT birthday FROM Members WHERE region = 'GA') Correct! =ANY is equivalent to IN 4. Subqueries and joins CS1222-S2019
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.