JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Introduction to Structured Query Language (SQL)
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
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.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Introduction to SQL Structured Query Language Martin Egerhill.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
RDB/1 An introduction to RDBMS Objectives –To learn about the history and future direction of the SQL standard –To get an overall appreciation of a modern.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
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 Joins Oracle and ANSI Standard SQL Lecture 6.
Chapter 9 Joining Data from Multiple Tables
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.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Copyright © Curt Hill Joins Revisited What is there beyond Natural Joins?
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Multiple Table Queries (Inner Joins, Equijoins) Week 3.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
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.
More SQL: Complex Queries,
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Oracle Join Syntax.
Basic Data Manipulation - Reading Data
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
David M. Kroenke and David J
Displaying Data from Multiple Tables Using Joins
Chapter Name SQL: Data Manipulation
JOINS (Joinining multiple tables)
Oracle Join Syntax.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
(SQL) Displaying Data from Multiple Tables
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
JOINS (Joinining multiple tables)
Presentation transcript:

JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data from 2 tables via the SQL92 Join syntax –What is an INNER JOIN? Is there a default JOIN? –CROSS Joins (SQL92) –SQL92 & SQL89 comparison and key differences –Oracle considerations –Using table aliases –Joining more than two Tables –Composite Joins –Practical 5-1 –Outer Joins –Practical 5-2

JOI/2 Both tables appear in FROM clause, sequence irrelevant Link the tables in the FROM clause with the keyword JOIN The ‘ON’ sub-clause relates a row of one table to a row of the other table, via one or more columns –Note the tablename.columnname syntax to avoid ambiguity No ability to predefine or store this join relationship in DBMS Retrieving data from two tables (SQL92) SELECT salesperson.dept_no, dept_name, lname FROM salesperson JOIN dept ON salesperson.dept_no = dept.dept_no ** NOTE ** Not ORACLE!

JOI/3 Joining – the one-to-many emp_no fname lname dept_no 10fred smith 1 20bob james 1 30 sue brown 2 dept_no dept_name 1 marketing 2 sales salesperson (‘many’) dept (‘one’) Foreign Key Primary Key SELECT * FROM salesperson JOIN dept ON salesperson.dept_no = dept.dept_no emp_no fname lname dept_no dept_no dept_name 10fred smith11marketing 20bob james11marketing 30 sue brown22sales result set ** NOTE ** Not ORACLE!

JOI/4 In chapter 2 we considered why we ‘normalise’ data into many tables –To avoid INSERT/UPDATE/DELETE anomalies and data duplication Downside of ‘normalisation’ is we have to do JOINS –JOINS are ‘realtime denormalisation’ –The JOIN effectively puts all the ‘dept’ data back –produces information that is useful and meaningful to the business –(refer back to chapter 2 page 6 and page 5!) Joining as De-Normalisation

JOI/5 Some RDBMS’s require the word INNER (or an alternative!) If a DBMS allows omission of ‘INNER’ it will default to ‘INNER’ Meaning: SELECT only the rows where there is a match between the two columns referred to in the ON clause Sometimes referred to as “equi-joins” The INNER JOIN (SQL92) SELECT salesperson.dept_no, dept_name, lname FROM salesperson INNER JOIN dept ON salesperson.dept_no = dept.dept_no ** NOTE ** Not ORACLE!

JOI/6 CROSS Joins –No ON sub-clause –Will simply join every row of one table with every row of the other –4 depts * 6 salespeople = 24 combinations –400 depts * people = ‘a long running query’ = embarrassment! –The answer set is known as a Cartesian Product –Rarely used as the answer set is normally meaningless –Could generate test data quickly via INSERT INTO tableX SELECT column_list FROM tableY CROSS JOIN tableZ The CROSS JOIN (SQL92) SELECT columnlist -- which column(s) are irrelevant FROM salesperson CROSS JOIN dept ** NOTE ** Not ORACLE!

JOI/7 SQL92 and SQL89 JOINS compared These two queries produce exactly the same results: –SQL92 –SQL89 Note that Oracle only supports the ‘89 version! –virtually all DBMS’s that support ‘92 still continue to support ’89 –much existing code uses ’89 syntax SELECT salesperson.dept_no, dept_name, lname FROM salesperson INNER JOIN dept ON salesperson.dept_no = dept.dept_no WHERE county = ‘Surrey’ SELECT salesperson.dept_no, dept_name, lname FROM salesperson, dept WHEREsalesperson.dept_no = dept.dept_no AND county = ‘Surrey’ Nearly all DBMS’s but not Oracle Nearly all DBMS’s and Oracle

JOI/8 SQL92 and SQL89 JOINS - Key differences FROM clause –SQL92 uses [INNER] JOIN; SQL89 uses a comma to separate table names ON sub-clause –SQL92 must have one (assuming it is not a CROSS JOIN); SQL89 never has one WHERE clause –SQL92 doesn’t need it for the JOIN logic as it is coded in the ON clause –SQL89, required to hold the ‘join’ logic; omit it and you get a Cartesian Product! SELECT salesperson.dept_no, dept_name, lname FROM salesperson, dept WHEREsalesperson.dept_no = dept.dept_no ANDcounty = ‘Surrey’ SQL 89 SELECT salesperson.dept_no, dept_name, lname FROM salesperson INNER JOIN dept ON salesperson.dept_no = dept.dept_no WHERE county = ‘Surrey’ SQL 92

JOI/9 Can be coded as Alias is (usually) optional, but saves some typing Renames table throughout the query –‘AS’ keyword is optional –Same in SQL92 and SQL89 –The alias appears to be used (in the SELECT clause) before you have defined it (in the FROM clause) but remember the DBMS must read the FROM first (and so should you!) SELECT salesperson.dept_no, dept_name, lname FROM salesperson JOIN dept ON salesperson.dept_no = dept.dept_no SELECT SP.dept_no, dept_name, lname FROM salesperson as SP JOIN dept as D ON SP.dept_no = D.dept_no Table Aliases

JOI/10 Aliasing and ambiguity considerations are identical Using ‘92 syntax, the 3rd and subsequent tables are JOINed via the next ON sub-clause to a table that has already been referenced In either syntax, ‘n’ JOINS require ‘n’ join conditions In ‘89 syntax one could easily omit a condition (part of the WHERE clause) and get a partial cartesian product - be careful! SELECT SP.dept_no, dept_name, lname FROM sale S JOIN salesperson SP ON S.emp_no = SP.emp_no JOIN dept D ON SP.dept_no = D.dept_no SELECT SP.dept_no, dept_name, lname FROM sale S, salesperson SP, dept D WHERES.emp_no = SP.emp_no ANDSP.dept_no = D.dept_no Joining more than two tables (‘92 & ‘89) SQL92 SQL89

JOI/11 The contact table has a composite primary key: SQL92 SELECT C.name, S.* -- meaning all the columns of sale FROM sale S JOIN contact C ON S.company_no = C.company_no AND S.contact_code = C.contact_code SQL-89 SELECT C.name, S.* FROM sale S, contact C WHERES.company_no = C.company_no AND S.contact_code = C.contact_code If you miss out half the join it will still run and produce unwanted extra rows. Such errors are not always readily detected. Composite Joins All parts of the key must be specified

JOI/12 Ch9Practical1 - Inner Joins Inner Joins on two or more tables You may write your code using ‘92 or ‘89 syntax (or both!) –Supplied solutions will include both –We would suggest using the ‘92 syntax unless –Oracle is your primary RDBMS or –You know that the majority of the code you will support is ‘89 syntax

JOI/13 The INNER JOIN selects all salespersons, but not managers of depts with no people The OUTER JOIN also includes rows that have no match –Left –Right –Full –With Null in the ‘many’ field SELECT manager, lname FROM dept D INNER JOIN salesperson SP ON D.dept_no = SP.dept_no Outer Joins (SQL92) SELECT manager, lname FROM dept D LEFT OUTER JOIN salesperson SP ON D.dept_no = SP.dept_no dept LEFT JOIN salesperson equals salesperson RIGHT JOIN dept Note

JOI/14 Consider: i.e ‘one’ RIGHT JOIN ‘many.’ –Would it mean anything? Produce any extra output? Outer Joins (General) dept RIGHT JOIN salesperson

JOI/15 Outer Joins (General) –If you add it reduces the output to just those companies who have not been sold to –It is therefore not always necessary to use –NOT EXISTS, NOT IN, (and we have been!) SELECT * FROM company C LEFT JOIN sale S ON C.company_no = S.company_no WHERE order_no is NULL

JOI/16 Outer Joins (Pre SQL92) MS SQL Server V6.0 (still supported as of V7): Oracle equivalent: SELECT manager, lname FROM dept D, salesperson SP WHERE D.dept_no *= SP.dept_no SELECT manager, lname FROM dept D, salesperson SP WHERED.dept_no = SP.dept_no(+) Note: The table with the ‘extra’ rows is often referred to as the OUTER table. Both Left Outer Joins

JOI/17 Ch9Practical2 - Outer Joins Outer Joins You will need to code these solutions using the SQL92 Outer Join syntax or the MS SQL Server preSQL92 proprietary method

JOI/18 Summary Joins –A mechanism to realtime denormalise tables to produce output There are different join types –In this chapter we looked at Inner, Cross and Outer joins –The syntax for Outer Joins is new to 92 although some DBMS’s had ways of achieving same prior to SQL92 Composite key joins –Sometimes we need to include several columns to join tables correctly Ambiguity is an issue to be addressed –Aliases help with ambiguity but are not essential