Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Manipulation Language.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 7:
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Sections 3 – Joins & Hierarchical Queries
Banner and the SQL Select Statement: Part Three (Joins) Mark Holliday Department of Mathematics and Computer Science Western Carolina University 4 November.
(with Microsoft SQL Server) Svetlin Nakov Telerik Corporation
CSC 405: Web Application And Engineering II7.1 Database Programming with SQL Aggregation and grouping with GROUP BY Aggregation and grouping with GROUP.
SQL 101 for Web Developers 14 November What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD:
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Microsoft Access 2013 ®® Tutorial 9 Using Action Queries and Advanced Table Relationships.
Database Programming Sections 4 – Joins. Marge Hohly2 Overview  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer.
Computer Science & Engineering 2111 Lecture 13 Outer Joins 1.
+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left.
U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER.
U:/msu/course/cse/103 Day 12, Slide 1 CSE 103 Students: Read Day 18 and learn about subqueries Others: Please save your.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries.
CS 122: Lecture 3 Joins (Part 1) Tarik Booker CS 122 California State University, Los Angeles October 7, 2014.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Lecture 7: Subqueries Tarik Booker California State University, Los Angeles.
Joins (Part II) Tarik Booker California State University, Los Angeles.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins.
Using Subqueries to Solve Queries
CS3220 Web and Internet Programming More SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
SQL – Subqueries.
Querying Multiple Tables
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Displaying Data from Multiple Tables
Manipulating Data Lesson 3.
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Presentation transcript:

Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II

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.

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

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

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);

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

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

Outer Join Order Join order makes a big difference A LEFT JOIN B is not the same as B LEFT JOIN A

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

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;

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;

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;

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

Full Join Example Select phone1.phone as firstphone, phone2.phone as secondphone From Phone1 Full Join Phone2 On Phone1.phone=phone2.phone firstphone secondphone NULL NULL NULL NULL NULL NULL NULL Phone 1 Phone 2

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

Typical Self Joins Employee Table: EmployeeID (primary key) FirstName LastName JobTitle Salary SupervisorID (foreign key references EmployeeID)

Typical Self Joins #1 List all employees (first and last names) who are supervised by no one.

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

Typical Self Joins #2 List all employees whose supervisors are supervised by no one.

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;

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;

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);

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)?

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?

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

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.

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.

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 Bush 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;