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

Slides:



Advertisements
Similar presentations
SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
Advertisements

Advanced SQL (part 1) CS263 Lecture 7.
© Abdou Illia MIS Spring 2014
Chapter 4 Joining Multiple Tables
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Displaying Data from Multiple Tables
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Chapter 6 Set Functions.
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 7: Subqueries and Set Operations.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lab 2: Single-Table Selections.
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 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
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 6: Midterm Review.
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 5: Functions.
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.
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.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Data Definition Language.
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.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Banner and the SQL Select Statement: Part Three (Joins) Mark Holliday Department of Mathematics and Computer Science Western Carolina University 4 November.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
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:
1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
SQL Select Statement IST359.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
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.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
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.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Joins (Part II) Tarik Booker California State University, Los Angeles.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins.
CS122 Using Relational Databases and SQL
Lyric Database Lyric Music
Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
03 | Querying Multiple Tables with Joins
David M. Kroenke and David J
JOINS (Joinining multiple tables)
CS122 Using Relational Databases and SQL
Lecture 3 Finishing SQL
JOINS (Joinining multiple tables)
Lyric Database Lyric Music
CS122 Using Relational Databases and SQL
Presentation transcript:

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.'