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

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

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.
© Abdou Illia MIS Spring 2014
Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Join Queries CS 146. Introduction: Join Queries So far, our SELECT queries have retrieved data from a single table Usually queries combine data from multiple.
Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Displaying Data from Multiple Tables
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
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.
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
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.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: Correlated Subqueries.
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 6: Midterm Review.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
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 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.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
Chapter 9 Joining Data from Multiple Tables
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
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)
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL Select Statement IST359.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
CS 122: Lecture 3 Joins (Part 1) Tarik Booker CS 122 California State University, Los Angeles October 7, 2014.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Lecture 7: Subqueries Tarik Booker California State University, Los Angeles.
IFS180 Intro. to Data Management Chapter 10 - Unions.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins.
DQL Statements Lab - 3 COMP 353 Summer
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Using the Set Operators
David M. Kroenke and David J
Using the Set Operators
CS122 Using Relational Databases and SQL
CS4222 Principles of Database System
CS122 Using Relational Databases and SQL
Contents Preface I Introduction Lesson Objectives I-2
CS122 Using Relational Databases and SQL
Database Systems: Design, Implementation, and Management Tenth Edition
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Using the Set Operators
CS122 Using Relational Databases and SQL
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 3: Joins Part I

Miscellany Questions regarding Lab and Homework #1? Very happy with Lab and Homework #1 Updated Wiki page Moved Functions past midterm Moved Subqueries and Set Operations before midterm

The Problem Question: Display each title name along with the name of the artist that made it. Hmmm... let's try SELECT Title, ArtistID from Titles; Hmmm... doesn't work We want artist names too Artist names, however, are in another table

The Solutions Use a subquery SELECT Title, (SELECT ArtistName FROM Artists WHERE ArtistID=T.ArtistID) AS 'ArtistName' FROM Titles T; Use a join SELECT Title, ArtistName FROM Titles NATURAL JOIN Artists;

What is a Join? A join is a subset of the Cartesian Product between two tables

What is a Cartesian Product? The Cartesian Product of tables A and B is the set of all possible concatenated rows whose first component comes from A and whose second component comes from B

Cartesian Product Example Given these two tables, what is the Cartesian Product? SELECT ArtistName FROM Artists; SELECT Title FROM Titles;

Cartesian Product Result Cartesian Product SELECT ArtistName, Title FROM Artists, Titles; 66 total rows Is this the answer we want? No! There is too much information. Cartesian Products can be quite large

Join Conditions Since many records in a Cartesian Product are not meaningful, we can eliminate them using a join condition In general, most of the time, we want to keep only matching records (i.e. only when two values of a common attribute between the two tables are equal)

Join Condition Example For example: -- Two tables SELECT * FROM Artists; SELECT * FROM Titles; -- Cartesian Product SELECT * FROM Artists, Titles; -- Join (Equi-join) SELECT * FROM Artists A, Titles T WHERE A.ArtistID=T.ArtistID;

Table Aliases When joining tables with common attribute names, MySQL will get confused if you say: SELECT * FROM Artists, Titles WHERE ArtistID=ArtistID; To solve this we can give each table an alias name (unlike column aliases, do not wrap you names in quotes): SELECT * FROM Artists A, Titles T WHERE A.ArtistID=T.ArtistID;

Ways to Do a Cartesian Product Several ways to do a Cartesian Product: Cartesian Product (Form #1: Equi-Join Syntax) SELECT * FROM Titles, Artists; Cartesian Product (Form #2: Cross Join Syntax) SELECT * FROM Titles CROSS JOIN Artists; Cartesian Product (Form #3: Inner Join Syntax) SELECT * FROM Titles INNER JOIN Artists; Cartesian Product (Form #4: Join On/Using Syntax) SELECT * FROM Titles JOIN Artists;

Cartesian Product Warnings Do not do a Cartesian Product on more than two tables unless you really know what youre doing! -- Takes a long time!!! SELECT * FROM Artists, Titles, Tracks;

MySQL Join Types Natural (this week) Equi- (this week) Inner (this week) Outer (next week) Left Right Cross (this week)

Natural Joins A Natural Join joins two or tables, automatically determining the join condition. The join condition attributes are only displayed once when using SELECT * with a natural join.

Natural Join Syntax Two tables: SELECT attribute_list FROM table1 NATURAL JOIN table2; Multiple tables: SELECT attribute_list FROM table1 NATURAL JOIN table2 NATURAL JOIN table3...

Natural Join Examples Two tables: SELECT * FROM Artists NATURAL JOIN Titles; Three tables: SELECT * FROM Artists NATURAL JOIN Titles NATURAL JOIN Tracks;

Equi-Joins An equi-join is a Cartesian Product with a join condition specified in the WHERE clause The join condition attributes will be displayed multiple times when using SELECT * with an equi-join. You must use table aliases in the join condition to differentiate join attributes.

Equi-Join Syntax Two tables: SELECT attribute_list FROM table1 alias1, table2 alias2 WHERE alias1.attribute = alias2.attribute; Multiple tables: SELECT attribute_list FROM table1 alias1, table2 alias2, table3 alias3,... WHERE alias1.attribute = alias2.attribute AND alias2.attribute = alias3.attribute AND...;

Equi-Join Examples Two tables: SELECT * FROM Artists A, Titles T WHERE A.ArtistID = T.ArtistID; Three tables: SELECT * FROM Artists A, Titles T, Tracks K WHERE A.ArtistID = T.ArtistID AND T.TitleID = K.TitleID;

Inner Joins Exact same thing as an equi-join, just using a different syntax: the JOIN ON syntax The INNER keyword is optional

Inner Join Syntax Two tables: SELECT attribute_list FROM table1 alias1 [INNER] JOIN table2 alias2 ON alias1.attribute = alias2.attribute; Multiple tables: SELECT attribute_list FROM table1 alias1 [INNER] JOIN table2 alias2 ON alias1.attribute = alias2.attribute [INNER] JOIN table3 alias3 ON alias2.attribute = alias3.attribute...

Inner Join Examples Two tables: SELECT * FROM Artists A INNER JOIN Titles T ON A.ArtistID = T.ArtistID; Three tables: SELECT * FROM Artists A INNER JOIN Titles T ON A.ArtistID = T.ArtistID INNER JOIN Tracks K ON T.TitleID = K.TitleID;

Join Using Equivalent to a natural join, with the exception that the attributes to be used in the join condition are not determined automatically The user must specify one or more column attributes for the join condition

Join Using Syntax Two tables: SELECT attribute_list FROM table1 JOIN table2 USING(attribute_name); Multiple tables: SELECT attribute_list FROM table1 JOIN table2 USING(attribute_name1) JOIN table3 USING(attribute_name2)...

Join Using Examples Two tables: SELECT * FROM Artists JOIN Titles USING(ArtistID); Three tables: SELECT * FROM Artists JOIN Titles USING(ArtistID) JOIN Tracks USING(TitleID);

Cross Joins A cross join computes the Cartesian Product A cross join is logically equivalent to: SELECT * FROM table1, table2, table3,...; In MySQL, a cross join is equivalent to an inner join, using the same syntax, but uses the CROSS keyword instead of INNER keyword In standard SQL2003, a cross join is not equivalent to an inner join. A cross join in standard SQL2003 cannot use the JOIN ON syntax.

Cross Join Syntax Two tables: SELECT attribute_list FROM table1 CROSS JOIN table2; Multiple tables: SELECT attribute_list FROM table1 CROSS JOIN table2 CROSS JOIN table3...