Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II
Miscellany Lab and Homework #2 Questions? Instructor = Pretty Happy
Topics for Today Problem Solving (Page 80) Joining on Multiple Columns (Pages ) Self Joins (Pages ) Left and Right Joins (Pages ) Mixed Joins (Pages )
Problem Solving Step #1: Analyze the question Single table? Multiple tables? Step #2: Determine the relationships Think logically using the relationship diagram Step #3: Join tables Join tables in logical order Step #4: Filter results Use the WHERE clause to filter results
Joining on Multiple Columns Select all artists and studios from the same city. SELECT ArtistName, StudioName FROM Artists A, Studios S WHERE A.City = S.City AND A.Region=S.Region; SELECT ArtistName, StudioName FROM Artists JOIN Studios USING(City, Region); SELECT ArtistName, StudioName FROM Artists A JOIN Studios S ON A.City = S.City AND A.Region=S.Region;
Self Joins A self join is a table that is joined to itself Can be used when a primary key and a foreign key exists within the same table Lyric Database: SalesPeople table Useful for modeling hierarchical relationships withing a single entity Folders: Parent-Child Hierarchy Employees: Employee Hierarchy Only works with Equi-Join and Join On syntax
Self Join Example List the first name of all sales people along with the first name of their supervisors. Look at the SalesPeople table SELECT * FROM SalesPeople; SELECT FirstName, Supervisor FROM SalesPeople; Then build your solution SELECT S1.FirstName, S2.FirstName FROM SalesPeople S1, SalesPeople S2 WHERE S1.Supervisor = S2.SalesID;
Outer Joins: The Problem Question: Display a list of artists who do not have a title. Associates each artist with his own titles... SELECT Title, ArtistName FROM Titles T, Artists A WHERE T.ArtistID = A.ArtistID; Associates each artist with titles by other artists... SELECT Title, ArtistName FROM Titles T, Artists A WHERE T.ArtistID <> A.ArtistID; Neither query give us what we want!
Outer Joins: The Solution Use an outer join SELECT ArtistName FROM Artist NATURAL LEFT JOIN Titles WHERE Title IS NULL; SELECT ArtistName FROM Artist LEFT JOIN Titles USING(ArtistID) WHERE Title IS NULL; SELECT ArtistName FROM Artist A LEFT JOIN Titles T ON A.ArtistID = T.ArtistID WHERE T.Title IS NULL; Can't use equi-join
Outer Join Types Left Join Every record from the left (first) table will always be listed at least once If a matching record is found in the right (second) table, it is listed normally (same as inner join) If there are no matching records to be found in the right (second) table (zero-matching rows), the record from the left table is still reported, albeit it is associated with NULL values in the right table. Right Join Same as left join, but swapping left and right
Left Join Syntax Two tables SELECT attribute_list FROM table1 LEFT JOIN table2 ON join_condition; More than two tables SELECT attribute_list FROM table1 LEFT JOIN table2 ON join_condition LEFT JOIN table3 ON join_condition...
Right Join Syntax Two tables SELECT attribute_list FROM table1 RIGHT JOIN table2 ON join_condition; More than two tables SELECT attribute_list FROM table1 RIGHT JOIN table2 ON join_condition RIGHT JOIN table3 ON join_condition...
More Left/Right Join Syntax NATURAL JOIN syntax SELECT attribute_list FROM table1 NATURAL LEFT JOIN table2; SELECT attribute_list FROM table1 NATURAL RIGHT JOIN table2; JOIN USING syntax SELECT attribute_list FROM table1 LEFT JOIN table2 USING(attribute); SELECT attribute_list FROM table1 RIGHT JOIN table2 USING(attribute);
Left Join Examples Left Join Example SELECT * FROM Artists A LEFT JOIN Titles T ON A.ArtistID = T.ArtistID; SELECT * FROM Titles T LEFT JOIN Artists A ON A.ArtistID = T.ArtistID; As you can see... table order matters!
Right Join Examples Right Join Example SELECT * FROM Artists A RIGHT JOIN Titles T ON A.ArtistID = T.ArtistID; SELECT * FROM Titles T RIGHT JOIN Artists A ON A.ArtistID = T.ArtistID; As you can see... table order matters!
How Outer Joins Work I Do you really want to know? SELECT A.ArtistName AS 'ArtistName', A.ArtistID AS 'ArtistID1', T.ArtistID AS 'ArtistID2', T.Title AS 'Title' FROM Artists A, Titles T WHERE A.ArtistID = T.ArtistID UNION SELECT DISTINCT A.ArtistName, A.ArtistID, NULL, NULL FROM Artists A, Titles T WHERE A.ArtistID <> T.ArtistID AND A.ArtistID NOT IN (SELECT A.ArtistID FROM Artists A, Titles T WHERE A.ArtistID = T.ArtistID) ORDER BY ArtistID1; Let's quickly go through this to see how a LEFT JOIN really works (not the code, but the results)
How Outer Joins Work II Now compare that to this, which gives you the same results SELECT A.ArtistName AS 'ArtistName', A.ArtistID AS 'ArtistID1', T.ArtistID AS 'ArtistID2', T.Title AS 'Title' FROM Artists A LEFT JOIN Titles T ON A.ArtistID = T.ArtistID ORDER BY ArtistID1; So now I hope you see that an outer join includes all the records from an inner join, plus records from unmatched rows
Mixed Joins It is OK to use a mixed join syntax SELECT M.FirstName, M.LastName, CASE WHEN G.Genre IS NULL THEN 'unknown' ELSE G.Genre END AS 'Genre' FROM Members M INNER JOIN XrefArtistsMembers X ON M.MemberID = X.MemberID INNER JOIN Artists A ON X.ArtistID = A.ArtistID LEFT JOIN Titles T ON A.ArtistID = T.ArtistID LEFT JOIN Genre G ON T.Genre = G.Genre;
Outer Join Tips and Techniques Always construct joins one-by-one, on a left-to- right basis, asking yourself, “What type of join do I need here?” When deciding on a join type, ask yourself the following question: Do I need to show zero-matching records? If yes, use an outer join. If no, use an equi- or inner join.
Sample Problems Report the names of all artists that came from e- mail that have not recorded a title. For ALL members, list thier first name, last name, and their associated genre. If a member doesn't have an associated genre, show the genre as 'unknown.'