Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Joins Oracle and ANSI Standard SQL Lecture 6.

Similar presentations


Presentation on theme: "SQL Joins Oracle and ANSI Standard SQL Lecture 6."— Presentation transcript:

1 SQL Joins Oracle and ANSI Standard SQL Lecture 6

2 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

21 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(+);

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

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

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

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

26 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(+);

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

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

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

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

31 Copyright 2006Page 31 SQL Joins Defining Join Types: Inheritance

32 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


Download ppt "SQL Joins Oracle and ANSI Standard SQL Lecture 6."

Similar presentations


Ads by Google