Download presentation
Presentation is loading. Please wait.
Published byElmer Bennett Modified over 9 years ago
1
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II
2
Outer Join Recall that Inner Join compares all records of tables being joined, matches rows based on shared fields and reports matching rows Outer Joins are a way to make SQL also show you rows that do not have matches in other tables. Don’t worry about the DBMS-specific “second” and “third” syntax forms in the E-Book. Just learn the syntax we cover in this lecture. Two kinds of Outer Joins: Left Joins & Right Joins.
3
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 in the same way as with inner join If there are no matching records to be found in the right (second) table (zero-matching records), the record from the left table is reported with NULL values in the right table. Right Join Same as left join, but swapping left and right
4
Outer Join Tips Left Join may be more intuitive than Right Join Mathematical expressions and western languages read from left to right… therefore most people find LEFT JOIN easier to understand
5
Outer Join Syntax SELECT attribute_list FROM table1 LEFT JOIN table2 ON join_condition; JOIN USING syntax SELECT attribute_list FROM table1 RIGHT JOIN table2 USING(attribute);
6
Inner Joins vs. Outer Joins Select Artistname, Title From Artists A Inner Join Titles T ON A.ArtistID=T.ArtistID Artistname Title -------------------------------- ---------------------------- The Neurotics Meet the Neurotics Confused Smell the Glove The Bullets Time Flies The Neurotics Neurotic Sequel Sonata Sonatas Louis Holiday Louis at the Keys
7
Inner Joins vs. Outer Joins Select Artistname, Title From Artists A Left Join Titles T ON A.ArtistID=T.ArtistID Artistname Title --------------------------------- ----------------------------- The Neurotics Meet the Neurotics The Neurotics Neurotic Sequel Louis Holiday Louis at the Keys Word NULL Sonata Sonatas The Bullets Time Flies Jose MacArthur NULL Confused Smell the Glove The Kicks NULL Today NULL 21 West Elm NULL Highlander NULL
8
Outer Join Order Join order makes a big difference A LEFT JOIN B is not the same as B LEFT JOIN A
9
Extracting Only Unmatched Data From Outer Joins To extract the “zero-matching” results from an outer join, you must test the primary key (from the opposite side of the outer join) for NULL For example, for A LEFT JOIN B, to extract the records in A that have no matches in B, you must test the primary key in B (the right table) for NULL in the WHERE clause
10
Extracting Unmatched Data From Outer Joins Preliminary: List all artists and the titles they have recorded, including those who have not recorded anything: Select A.ArtistName, T.Title from Artists A left join Titles T using (ArtistID) Review “is null” List only artists who have not recorded anything: Select A.ArtistName, T.Title from Artists A left join Titles T using (ArtistID) where T.TitleID is null;
11
Extracting Unmatched Data From Outer Joins PAY ATTENTION TO THIS! MANY STUDENTS GET THESE QUESTIONS WRONG ON EXAMS! To find records that do not match specific values in another table: add the additional condition into the ON or WHERE clause of the join then filter out records with non-null values Example: find titles that do not contain any tracks longer than 400 seconds: SELECT T.title FROM titles T LEFT JOIN tracks TR on(T.titleID = TR.titleID AND TR.lengthseconds > 400) WHERE lengthSeconds IS NULL;
12
Extracting Unmatched Data From Outer Joins The last query is NOT the same as this one: Find all titles with tracks that are not longer than 400 seconds: SELECT DISTINCT T.title FROM titles T JOIN tracks TR on(T.titleID = TR.titleID) WHERE lengthSeconds <= 400;
13
Full Join Outer Join reports all records in one table plus matching records in a second table Full Join reports all records in both tables whether they match or not Essentially a Left Join and a Right Join at the same time MYSQL does not support full joins
14
Full Join Example Select phone1.phone as firstphone, phone2.phone as secondphone From Phone1 Full Join Phone2 On Phone1.phone=phone2.phone firstphone secondphone ---------- ----------- 1112223333 NULL 2223334444 NULL 3334445555 NULL 4445556666 NULL 5556667777 6667778888 7778889999 NULL 0001112222 NULL 9990001111 NULL 8889990000 Phone 1 Phone 2
15
Self Joins A self join is a join between two or more instances of the same table Typical case is when a foreign key references a primary key in the same table Non-typical case is when you want to find pairs of things from within the same table Useful for modelling hierarchical relationships within a single table Folders: Parent-Child Hierarchy Employees: Employee Hierarchy
16
Typical Self Joins Employee Table: EmployeeID (primary key) FirstName LastName JobTitle Salary SupervisorID (foreign key references EmployeeID)
17
Typical Self Joins #1 List all employees (first and last names) who are supervised by no one.
18
Typical Self Joins #1 List all employees (first and last names) who are supervised by no one. Select FirstName, LastName from employee E where E.SupervisorID is null; Self-join not needed yet
19
Typical Self Joins #2 List all employees whose supervisors are supervised by no one.
20
Typical Self Joins #2 List all employees whose supervisors are supervised by no one. SELECT E.FirstName, E.LastName FROM Employee E Left JOIN Employee S ON E.SupervisorID = S.EmployeeID WHERE S.SupervisorID IS NULL;
21
Typical Self Joins #3 What does this one get us? SELECT E.FirstName, E.LastName FROM Employee E JOIN Employee S ON E.SupervisorID = S.EmployeeID JOIN Employee SS ON S.SupervisorID = SS.EmployeeID WHERE SS.SupervisorID IS NULL;
22
Self Joins Consider this: create table presidents ( ID int primary key, name varChar(25), predecessorID int ); insert into presidents values(44, "Obama", 43); insert into presidents values(43, "Bush, GW", 42); insert into presidents values(42, "Clinton", 41); insert into presidents values(41, "Bush, GHW", null);
23
Self Joins How can we list the name of each president with that of his predecessor? How do we get the first president listed in our output? What about listing each with the name of his successor? How do we get the presidents and their successors while showing the last entry in the table (the one with successorID null)?
24
Self Joins for Pairs of Records List all pairs of titles for which both titles were recorded at the same studio Naïve solution: Select T1.Title, T2.Title from Titles T1 join Titles T2 on T1.StudioID = T2.StudioID What will we get? Somewhat better solution: Select T1.Title, T2.Title from Titles T1 join Titles T2 on T1.StudioID = T2.StudioID where T1.Title <> T2.Title What will we get?
25
Self Joins for Pairs of Records List all pairs of titles for which both titles were recorded at the same studio The right solution: Select T1.Title, T2.Title from Titles T1 join Titles T2 on T1.StudioID = T2.StudioID where T1.Title < T2.Title
26
Think carefully about your data! Find all artists that not recorded jazz albums This is WRONG: SELECT distinct A.Artistname FROM Artists A JOIN Titles T ON (A.ArtistID = T.ArtistID) WHERE T.Genre != "jazz"; *WRONG* This is CORRECT: SELECT distinct A.Artistname FROM Artists A LEFT JOIN Titles T ON (A.ArtistID = T.ArtistID AND T.Genre = "jazz") WHERE T.TitleID IS NULL; The first one returns artists which have recorded non-jazz titles; that is not the same as artists which have not recorded jazz titles.
27
Think carefully about your data! Because the database we use is small, you can’t always catch errors like this by simply checking to see that the output is correct! Q: list all members who do not belong to the Bullets *WRONG* SELECT M.MemberID, M.lastname, A.ArtistName FROM Members M JOIN XrefArtistsMembers X using(MemberID) JOIN Artists A USING (ArtistID) WHERE A.Artistname != "The Bullets"; *WRONG* *CORRECT* SELECT M.MemberID, M.lastname FROM Members M LEFT JOIN XrefArtistsMembers X ON(M.MemberID = X.MemberID) LEFT JOIN Artists A ON (X.ArtistID = A.ArtistID AND A.Artistname = "The Bullets") WHERE A.Artistid IS NULL; These two queries return the same results (though possibly in different orders), but the first one is still wrong. If the DB contained members who did not belong to any artists, it would return erroneous results.
28
In Class Exercise Write a SQL query to find the ID of the president whose predecessor was the first in the sequence listed in the DB. The table is named presidents. The data is as follows: ID lastName predecessorID 41 Bush null 42 Clinton 41 43 Bush 42 44 Obama 43 Example from slide 13: SELECT E.FirstName, E.LastName FROM Employee E JOIN Employee S ON E.SupervisorID = S.EmployeeID WHERE S.SupervisorID IS NULL;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.