SQL Joins Oracle and ANSI Standard SQL Lecture 6.

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

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.
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
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
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.
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using.
SQL Joins.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 9 Joining Data from Multiple Tables
Sections 3 – Joins & Hierarchical Queries
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
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.
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.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
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.
劉 志 俊 (Chih-Chin Liu) 中華大學 資訊工程系 October 2001 Chap 9 SQL (III): Advanced Queries.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Agenda for 02/16/2006 Answer any questions about SQL project. Do you want to see any of the answers for the queries? Discuss additional formatting options.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Copyright © Curt Hill Joins Revisited What is there beyond Natural Joins?
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Database Programming Sections 4 – Joins. Marge Hohly2 Overview  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
SQL Select Statement IST359.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
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.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Oracle Join Syntax.
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
David M. Kroenke and David J
Displaying Data from Multiple Tables Using Joins
JOINS (Joinining multiple tables)
Oracle Join Syntax.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Contents Preface I Introduction Lesson Objectives I-2
Database Systems: Design, Implementation, and Management Tenth Edition
SQL set operators and modifiers.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
JOINS (Joinining multiple tables)
Presentation transcript:

SQL Joins Oracle and ANSI Standard SQL Lecture 6

Copyright 2006Page 2 SQL Joins Join Types Inner join or equijoin Inner join or equijoin Surrogate keys Surrogate keys Natural keys Natural keys Non-equijoin Non-equijoin Outer join Outer join Full outer join Full outer join Self join Self join Join behavior inheritance tree Join behavior inheritance tree

Copyright 2006Page 3 SQL Joins Defining Join Types: INNER JOIN

Copyright 2006Page 4 SQL Joins Defining Join Types: INNER JOIN An INNER JOIN is also an equijoin, or equality join between equals. An INNER JOIN is also an equijoin, or equality join between equals. An INNER JOIN matches on one or a set of columns values from one table: An INNER JOIN matches on one or a set of columns values from one table: When one table is involved, an INNER JOIN creates an intersection between two copies of a single table (typically done with two different column names). When one table is involved, an INNER JOIN creates an intersection between two copies of a single table (typically done with two different column names). When two or more tables are involved, an INNER JOIN creates an intersection between the tables based on designated column names. When two or more tables are involved, an INNER JOIN creates an intersection between the tables based on designated column names.

Copyright 2006Page 5 SQL Joins Defining Join Types: INNER JOIN Oracle Syntax: Oracle Syntax: You create an INNER JOIN by using an equality statement between two columns, or sets of two columns, between two copies of a table, or two tables, in the WHERE clause of a SQL statement. You create an INNER JOIN by using an equality statement between two columns, or sets of two columns, between two copies of a table, or two tables, in the WHERE clause of a SQL statement. There is only one syntax for an INNER JOIN in Oracle Proprietary SQL. There is only one syntax for an INNER JOIN in Oracle Proprietary SQL. Equijoin resolutions are done by using an equality comparison between column values from two copies of one table or two table in the SQL WHERE clause, likewise this applies to sets of columns placed in the WHERE clause. Equijoin resolutions are done by using an equality comparison between column values from two copies of one table or two table in the SQL WHERE clause, likewise this applies to sets of columns placed in the WHERE clause.

Copyright 2006Page 6 SQL Joins Defining Join Types: INNER JOIN ANSI SQL Syntax: ANSI SQL Syntax: You create an INNER JOIN by placing a position specific set of tables in the FROM clause followed by an ON or USING clause. You create an INNER JOIN by placing a position specific set of tables in the FROM clause followed by an ON or USING clause. Equality statements are between one or more columns in two copies of one table or two tables: Equality statements are between one or more columns in two copies of one table or two tables: When the columns share the same name and data type, you use the USING clause. When the columns share the same name and data type, you use the USING clause. When the columns have different names but the same data type, you use the ON clause. When the columns have different names but the same data type, you use the ON clause. If only the word JOIN is used, an INNER JOIN is assumed by the SQL parser. If only the word JOIN is used, an INNER JOIN is assumed by the SQL parser.

Copyright 2006Page 7 SQL Joins Defining Join Types: INNER JOIN Oracle Example: Oracle Example: SELECT a.column1, b.column2 FROM table1 a, table2 b WHERE a.columnpk = b.columnfk;

Copyright 2006Page 8 SQL Joins Defining Join Types: INNER JOIN ANSI SQL Example: ANSI SQL Example: SELECT a.column1, b.column2 FROM table1 a [INNER] JOIN table2 b ON a.columnpk = b.columnfk;

Copyright 2006Page 9 SQL Joins Defining Join Types: INNER JOIN ANSI SQL Example: ANSI SQL Example: SELECT a.column1, b.column2 FROM table1 a [INNER] JOIN table2 b USING(same_column_name);

Copyright 2006Page 10 SQL Joins Defining Join Types: Natural Join Is an equijoin or a match on one or a set of columns from one or more tables. Is an equijoin or a match on one or a set of columns from one or more tables. An equijoin creates an intersection based on columns with the same names. An equijoin creates an intersection based on columns with the same names. Oracle Syntax: Oracle Syntax: None exists. None exists. ANSI SQL Syntax: ANSI SQL Syntax: You create an NATURAL JOIN by using the NATURAL key word. You create an NATURAL JOIN by using the NATURAL key word. A NATURAL JOIN uses columns with the same name when they exist. A NATURAL JOIN uses columns with the same name when they exist. A NATURAL JOIN returns a Cartesian product or CROSS JOIN when there are no matching column names. A NATURAL JOIN returns a Cartesian product or CROSS JOIN when there are no matching column names.

Copyright 2006Page 11 SQL Joins Defining Join Types: Natural Join ANSI SQL Example: ANSI SQL Example: SELECT a.column1, b.column2 FROM table1 a NATURAL JOIN table2 b;

Copyright 2006Page 12 SQL Joins Defining Join Types: Non-equijoin A non-equijoin is an indirect match: A non-equijoin is an indirect match: Occurs when one column value is found in the range between two other column values Occurs when one column value is found in the range between two other column values Uses the BETWEEN operator. Uses the BETWEEN operator. Also occurs when one column value is found by matching against a criterion using an inequality operator. Also occurs when one column value is found by matching against a criterion using an inequality operator.

Copyright 2006Page 13 SQL Joins Defining Join Types: Non-equijoin Oracle Example: Oracle Example: SELECT a.column1, b.column2 FROM table1 a, table2 b WHERE a.columnpk >= b.columnfk;

Copyright 2006Page 14 SQL Joins Defining Join Types: Non-equijoin Oracle Example: Oracle Example: SELECT a.column1, b.column2 FROM table1 a, table2 b WHERE a.cola BETWEEN b.colx AND b.coly;

Copyright 2006Page 15 SQL Joins Defining Join Types: Non-equijoin ANSI SQL Example: ANSI SQL Example: SELECT a.column1, b.column2 FROM table1 a CROSS JOIN table2 b WHERE a.columnpk >= b.columnfk;

Copyright 2006Page 16 SQL Joins Defining Join Types: Non-equijoin ANSI SQL Example: ANSI SQL Example: SELECT a.column1, b.column2 FROM table1 a CROSS JOIN table2 b WHERE a.column BETWEEN b.colx AND b.coly;

Copyright 2006Page 17 SQL Joins Defining Join Types: Outer Join

Copyright 2006Page 18 SQL Joins Defining Join Types: Outer Join A match that includes all the matches between two copies of one table or two tables, and A match that includes all the matches between two copies of one table or two tables, and All the non-matches from one copy of the single table, or one table of the two tables. All the non-matches from one copy of the single table, or one table of the two tables.

Copyright 2006Page 19 SQL Joins Defining Join Types: Outer Join Oracle Syntax: Oracle Syntax: The “(+)” symbol is used to create an OUTER JOIN. The “(+)” symbol is used to create an OUTER JOIN. When the “(+)” symbol is on the right of the join operand, it acts as the equivalent of a LEFT JOIN. When the “(+)” symbol is on the right of the join operand, it acts as the equivalent of a LEFT JOIN. When the “(+)” it is on the left of the join operand, it is the equivalent of a RIGHT JOIN. When the “(+)” it is on the left of the join operand, it is the equivalent of a RIGHT JOIN.

Copyright 2006Page 20 SQL Joins Defining Join Types: Outer Join ANSI Syntax: ANSI Syntax: These are defined by LEFT JOIN and RIGHT JOIN operators. These are defined by LEFT JOIN and RIGHT JOIN operators. Both LEFT [OUTER] JOIN and RIGHT [OUTER] JOIN are synonymous with LEFT JOIN and RIGHT JOIN respectively, the OUTER is assumed when left out. Both LEFT [OUTER] JOIN and RIGHT [OUTER] JOIN are synonymous with LEFT JOIN and RIGHT JOIN respectively, the OUTER is assumed when left out. The OUTER keyword is optional or implicitly derived by the SQL parser. The OUTER keyword is optional or implicitly derived by the SQL parser. These join semantics are unidirectional outer joins and synonymous or alike behaviors because the only difference is the direction as to how their behavior is applied. These join semantics are unidirectional outer joins and synonymous or alike behaviors because the only difference is the direction as to how their behavior is applied. The LEFT [OUTER] JOIN finds the non-matching values on the right-side of the operand. The LEFT [OUTER] JOIN finds the non-matching values on the right-side of the operand. The RIGHT [OUTER] JOIN finds the non-matching values on the left-side of the operand. The RIGHT [OUTER] JOIN finds the non-matching values on the left-side of the operand.

Copyright 2006Page 21 SQL Joins Defining Join Types: Outer Join Oracle Example (left join): Oracle Example (left join): SELECT a.column1, b.column2 FROM table1 a, table2 b WHERE a.columnpk = b.columnfk(+);

Copyright 2006Page 22 SQL Joins Defining Join Types: Outer Join Oracle Example (right join): Oracle Example (right join): SELECT a.column1, b.column2 FROM table1 a, table2 b WHERE a.columnpk(+) = b.columnfk;

Copyright 2006Page 23 SQL Joins Defining Join Types: Outer Join ANSI SQL Example: ANSI SQL Example: SELECT a.column1, b.column2 FROM table1 a LEFT JOIN table2 b ON a.columnpk = b.columnfk;

Copyright 2006Page 24 SQL Joins Defining Join Types: Outer Join ANSI SQL Example: ANSI SQL Example: SELECT a.column1, b.column2 FROM table1 a RIGHT JOIN table2 b ON a.columnpk = b.columnfk;

Copyright 2006Page 25 SQL Joins Defining Join Types: Full Outer Join A match that includes all matches between two tables plus all non-matches whether on the left or right side of a join. A match that includes all matches between two tables plus all non-matches whether on the left or right side of a join. Oracle syntax: There is no full outer join syntax. Oracle syntax: There is no full outer join syntax. Oracle syntax: The UNION operator to mimic the behavior. Oracle syntax: The UNION operator to mimic the behavior. ANSI SQL synatx: Uses the FULL [OUTER] JOIN operator. ANSI SQL synatx: Uses the FULL [OUTER] JOIN operator.

Copyright 2006Page 26 SQL Joins Defining Join Types: Outer Join Oracle Example: Oracle Example: SELECT a.column1, b.column2 FROM table1 a, table2 b WHERE a.columnpk(+) = b.columnfk UNION SELECT a.column1, b.column2 FROM table1 a, table2 b WHERE a.columnpk = b.columnfk(+);

Copyright 2006Page 27 SQL Joins Defining Join Types: Full Join ANSI SQL Example: ANSI SQL Example: SELECT a.column1, b.column2 FROM table1 a FULL [OUTER] JOIN table2 b ON a.columnpk = b.columnfk;

Copyright 2006Page 28 SQL Joins Defining Join Types: Self Join A recursive join internally within a single table based on a primary and foreign key residing in each row of data in a table. A recursive join internally within a single table based on a primary and foreign key residing in each row of data in a table. You must use table name aliases to create a SELF JOIN. You must use table name aliases to create a SELF JOIN. Self joins typically use two separate column names. Self joins typically use two separate column names.

Copyright 2006Page 29 SQL Joins Defining Join Types: Self Join Oracle Example: Oracle Example: SELECT a.column1, b.column2 FROM table1 a, table1 b WHERE a.columnpk = b.columnfk;

Copyright 2006Page 30 SQL Joins Defining Join Types: Self Join ANSI SQL Example: ANSI SQL Example: SELECT a.column1, b.column2 FROM table1 a [INNER] JOIN table1 b ON a.columnpk = b.columnfk;

Copyright 2006Page 31 SQL Joins Defining Join Types: Inheritance

Copyright 2006Page 32 Summary Inner join or equijoin Inner join or equijoin Surrogate keys Surrogate keys Natural keys Natural keys Non-equijoin Non-equijoin Outer join Outer join Full outer join Full outer join Self join Self join Join behavior inheritance tree Join behavior inheritance tree