Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins.

Similar presentations


Presentation on theme: "CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins."— Presentation transcript:

1 CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins

2 4. Subqueries and joins CS122_W16 Outline r Subqueries m Subqueries in the WHERE and Having clause m Subqueries with IN, ALL & ANY r Joining tables 4-2

3 4. Subqueries and joins CS122_W16 Sub-Queries r A query within a query r Sub-query placed in parentheses r Sub-queries can be placed in m WHERE clause m HAVING clause m SELECT clause m FROM clause 4-3

4 4. Subqueries and joins CS122_W16 Sub-Queries examples (1) r List the name of the oldest member m Find the earliest birthday (1955-11-01) Select min(birthday) From Members m Find the member whose birthday is ‘1955-11-01’ Select firstname, lastname From Members Where birthday = ‘1955-11-01’ 4-4

5 4. Subqueries and joins CS122_W16 Sub-Queries examples (1) r Combine the two queries SELECT firstname, lastname FROM Members WHERE birthday = (SELECT Min(birthday) FROM Members ) 4-5

6 4. Subqueries and joins CS122_W16 Sub-Queries examples (2) r List all track titles and lengths of all tracks whose length is longer than the average of all track lengths m Find the average track lenghths (276.08) SELECT Avg(lengthseconds) FROM Tracks m Find the all track titles and lengths of all tracks whose length is greater than 276.08 SELECT tracktitle, lengthseconds FROM Tracks WHERE lengthseconds > 276.08 4-6

7 4. Subqueries and joins CS122_W16 Sub-Queries examples (2) r Combine the two queries SELECT tracktitle, lengthseconds FROM Tracks WHERE lengthseconds > ( SELECT Avg(lengthseconds) FROM Tracks ) 4-7

8 4. Subqueries and joins CS122_W16 Sub-Queries using IN r Report the name of all artists who have recorded a title SELECT artistName FROM Artists WHERE artistID IN (SELECT artistID FROM Titles) 4-8

9 Question r Is the following query correct? SELECT artistName FROM Artists WHERE artistID = (SELECT artistID FROM Titles) 4. Subqueries and joins CS122_W16 4-9

10 4. Subqueries and joins CS122_W16 ALL & ANY r Used with Sub-Queries r Can be used with greater than and less than tests r If using ANY, then comparison will be true if it satisfies any value produced by the sub-query r If using ALL, then comparison will be true if it satisfies all values produced by the sub-query. 4-10

11 4. Subqueries and joins CS122_W16 ALL example r List the name, region, and birthday of every member who is older than all of the members in Georgia SELECT lastname, firstname, region, birthday FROM Members WHERE birthday < ALL (SELECT birthday FROM Members WHERE region = ' GA ' ) 4-11

12 4. Subqueries and joins CS122_W16 ANY example r 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-12

13 4. Subqueries and joins CS122_W16 Exercises r Report the firstname, lastname and birthday of the members who has the same birthday as someone in Virginia r Is the following query correct? SELECT lastname, firstname, region, birthday FROM Members WHERE birthday = ALL (SELECT birthday FROM Members WHERE region = 'GA' ) It doesn’t make sense to test whether a single value equals a list of values 4-13

14 4. Subqueries and joins CS122_W16 Exercises (cont.) r 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 compare 3 with (3,4,5,6) 4-14

15 4. Subqueries and joins CS122_W16 Exercises (cont.) r 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-15

16 4. Subqueries and joins CS122_W16 Subquery in Having clause r Find the title id which has the longest average track length Select titleID, avg(lengthseconds) From Tracks Group by titleID Having avg(lengthseconds) >= all (select avg(lengthseconds) from Tracks group by titleID) 4-16

17 4. Subqueries and joins CS122_W16 Join r When information spreads over multiple tables m Find the projects which John is working on eidename 01John 02Susan eidpid 01T4 01X3 02S2 Employees Projects 4-17

18 4. Subqueries and joins CS122_W16 Step1: Cartesian product eidename eidpid John 01 X3 John 01 T4 John 01 02S2 01T4 Susan 02 01X3 Susan 02 S2Susan 02 EmployeesProjects Join every row in the first table to every row in the second table in every possible combination 4-18

19 4. Subqueries and joins CS122_W16 Step2: Throw out non-matching records pid eidename eid John 01 X3 John 01 T4 John 01 02S2 01T4 Susan 02 01X3 Susan 02 S2Susan 02 Non-matching records 4-19

20 4. Subqueries and joins CS122_W16 Results eidename eidpid John 01 X3 John 01 T4 02S2Susan 02 4-20

21 4. Subqueries and joins CS122_W16 The kinds of join r Equi Join r Inner join r Outer join m Left join m Right join m Full join r Self join r Cross join 4-21

22 4. Subqueries and joins CS122_W16 Equi Join example 1 r List the CD title and the title of all tracks recorded in Studio 2 SELECT title, tracktitle FROM Titles, Tracks WHERE Titles.titleID = Tracks.titleID AND studioID=2 Which tables to join Which fields to match (throw non-mathing fields 4-22

23 4. Subqueries and joins CS122_W16 Tips to join tables 1. Find which tables to join: TA, TB 2. Find the common attributes of those tables: field 3. Specify the relationship in the where clause: TA.field=TB.field 4. Put the two tables’ fields in the select clause as needed 4-23

24 4. Subqueries and joins CS122_W16 Equi join example 2 r List the names of all artists who have recorded a title and the number of titles they have SELECT artistname, COUNT(Titles.artistID) AS NumTitles FROM Artists, Titles WHERE Artists.artistID=Titles.artistID GROUP BY artistname 4-24

25 4. Subqueries and joins CS122_W16 Equi join summary r The tables to join are in the FROM clause m Only join just enough tables r The relationship specifications are in the WHERE clause r WHERE clause will get messy if more tables are added 4-25

26 4. Subqueries and joins CS122_W16 Inner Join r Same result as Equi Join r Different Syntax m Tables are listed with the keyword INNER JOIN m Relationship specifications moved from WHERE clause to ON clause Free WHERE clause for traditional conditions 4-26

27 4. Subqueries and joins CS122_W16 Inner Join example 1 r List the CD title and the title of all tracks recorded in Studio 2 SELECT title, tracktitle FROM Titles INNER JOIN Tracks ON Titles.titleid = Tracks.titleid WHERE studioID=2 Tables to join Relationship specification 4-27

28 4. Subqueries and joins CS122_W16 Inner Join example 2 r List the names of all artists who have recorded a title and the number of titles they have SELECT artistname, COUNT(Titles.artistID) AS NumTitles FROM Artists INNER JOIN Titles ON Artists.artistID=Titles.artistID GROUP BY artistName 4-28

29 4. Subqueries and joins CS122_W16 Table Aliases r Saves typing r Table name cannot be used in rest of query r Case-sensitive in some systems r Example m List the names of members from Georgia (GA) and their salespeople SELECT M.Lastname, M.FirstName, S.Lastname,S.Firstname FROM Members M, Salespeople S WHERE M.SalesID= S.SalesID And Region='GA' 4-29

30 4. Subqueries and joins CS122_W16 Summary r Sub-queries in WHERE clauses r Sub-queries with IN, ALL, ANY r Sub-queries in HAVING clauses r Equi-joins r Inner joins 4-30


Download ppt "CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins."

Similar presentations


Ads by Google