sql joins oracle and ansi standard sql

32
SQL Joins SQL Joins Oracle and ANSI Standard SQL Oracle and ANSI Standard SQL Lecture 6 Lecture 6

Upload: addison-pennington

Post on 30-Dec-2015

82 views

Category:

Documents


1 download

DESCRIPTION

SQL Joins Oracle and ANSI Standard SQL. Lecture 6. SQL Joins Join Types. Inner join or equijoin Surrogate keys Natural keys Non-equijoin Outer join Full outer join Self join Join behavior inheritance tree. SQL Joins Defining Join Types: INNER JOIN. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SQL Joins Oracle and ANSI Standard SQL

SQL JoinsSQL JoinsOracle and ANSI Standard SQLOracle and ANSI Standard SQL

Lecture 6Lecture 6

Page 2: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 22

SQL JoinsSQL Joins

Join TypesJoin Types Inner join or Inner join or equijoinequijoin

Surrogate keysSurrogate keys Natural keysNatural keys

Non-equijoinNon-equijoin Outer joinOuter join Full outer joinFull outer join Self joinSelf join Join behavior inheritance Join behavior inheritance

treetree

Page 3: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 33

SQL JoinsSQL Joins Defining Join Types: Defining Join Types: INNER INNER

JOINJOIN

A B

INTERSECTION

Page 4: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 44

SQL JoinsSQL Joins Defining Join Types: Defining Join Types: INNER INNER

JOINJOIN An An INNER JOININNER JOIN is also an is also an equijoinequijoin, or , or

equality join between equals.equality join between equals. An An INNER JOININNER JOIN matches on one or a set of matches on one or a set of

columns values from one table:columns values from one table: When one table is involved, an When one table is involved, an INNER JOININNER JOIN

creates an intersection between two copies of creates an intersection between two copies of a single table (typically done with two different a single table (typically done with two different column names).column names).

When two or more tables are involved, an When two or more tables are involved, an INNER JOININNER JOIN creates an intersection between creates an intersection between the tables based on designated column names.the tables based on designated column names.

Page 5: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 55

SQL JoinsSQL Joins Defining Join Types: Defining Join Types: INNER INNER

JOINJOIN Oracle Syntax:Oracle Syntax:

You create an You create an INNER JOININNER JOIN by using an equality by using an equality statement between two columns, or sets of two statement between two columns, or sets of two columns, between two copies of a table, or two columns, between two copies of a table, or two tables, in the tables, in the WHEREWHERE clause of a SQL statement. clause of a SQL statement.

There is only one syntax for an There is only one syntax for an INNER JOININNER JOIN in in Oracle Proprietary SQL.Oracle Proprietary SQL.

EquijoinEquijoin resolutions are done by using an resolutions are done by using an equality comparison between column values equality comparison between column values from two copies of one table or two table in the from two copies of one table or two table in the SQL SQL WHEREWHERE clause, likewise this applies to sets clause, likewise this applies to sets of columns placed in the of columns placed in the WHEREWHERE clause. clause.

Page 6: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 66

SQL JoinsSQL Joins Defining Join Types: Defining Join Types: INNER INNER

JOINJOIN ANSI SQL Syntax:ANSI SQL Syntax:

You create an You create an INNER JOININNER JOIN by placing a position by placing a position specific set of tables in the specific set of tables in the FROMFROM clause clause followed by an followed by an ONON or or USINGUSING clause. clause.

Equality statements are between one or more Equality statements are between one or more columns in two copies of one table or two columns in two copies of one table or two tables:tables:

When the columns share the same name and data When the columns share the same name and data type, you use the type, you use the USINGUSING clause. clause.

When the columns have different names but the When the columns have different names but the same data type, you use the same data type, you use the ONON clause. clause.

If only the word If only the word JOINJOIN is used, an is used, an INNER JOININNER JOIN is is assumed by the SQL parser. assumed by the SQL parser.

Page 7: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 77

SQL JoinsSQL Joins Defining Join Types: Defining Join Types: INNER INNER

JOINJOIN Oracle Example:Oracle Example:

SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a, table2 bFROM table1 a, table2 b

WHERE a.columnpk = b.columnfk;WHERE a.columnpk = b.columnfk;

Page 8: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 88

SQL JoinsSQL Joins Defining Join Types: Defining Join Types: INNER INNER

JOINJOIN ANSI SQL Example:ANSI SQL Example:

SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a [INNER] JOIN table2 bFROM table1 a [INNER] JOIN table2 b

ON a.columnpk = b.columnfk;ON a.columnpk = b.columnfk;

Page 9: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 99

SQL JoinsSQL Joins Defining Join Types: Defining Join Types: INNER INNER

JOINJOIN ANSI SQL Example:ANSI SQL Example:

SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a [INNER] JOIN table2 bFROM table1 a [INNER] JOIN table2 b

USING(same_column_name);USING(same_column_name);

Page 10: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 1010

SQL JoinsSQL Joins Defining Join Types: Natural Defining Join Types: Natural

JoinJoin Is an Is an equijoinequijoin or a match on one or a set of or a match on one or a set of

columns from one or more tables.columns from one or more tables. An An equijoinequijoin creates an intersection based on creates an intersection based on

columns with the same names.columns with the same names. Oracle Syntax:Oracle Syntax:

None exists.None exists. ANSI SQL Syntax:ANSI SQL Syntax:

You create an You create an NATURAL JOINNATURAL JOIN by using the by using the NATURALNATURAL key key word.word.

A A NATURALNATURAL JOINJOIN uses columns with the same name when uses columns with the same name when they exist.they exist.

A A NATURALNATURAL JOINJOIN returns a Cartesian product or returns a Cartesian product or CROSSCROSS JOINJOIN when there are no matching column names. when there are no matching column names.

Page 11: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 1111

SQL JoinsSQL Joins Defining Join Types: Natural Defining Join Types: Natural

JoinJoin ANSI SQL Example:ANSI SQL Example:

SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a NATURAL JOIN table2 b;FROM table1 a NATURAL JOIN table2 b;

Page 12: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 1212

SQL JoinsSQL Joins Defining Join Types: Non-Defining Join Types: Non-

equijoinequijoin A A non-equijoinnon-equijoin is an indirect match: is an indirect match:

Occurs when one column value is found Occurs when one column value is found in the range between two other column in the range between two other column valuesvalues

Uses the Uses the BETWEENBETWEEN operator. operator. Also occurs when one column value is Also occurs when one column value is

found by matching against a criterion found by matching against a criterion using an inequality operator.using an inequality operator.

Page 13: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 1313

SQL JoinsSQL Joins Defining Join Types: Non-equijoinDefining Join Types: Non-equijoin

Oracle Example:Oracle Example:SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a, table2 bFROM table1 a, table2 b

WHERE a.columnpk >= b.columnfk;WHERE a.columnpk >= b.columnfk;

Page 14: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 1414

SQL JoinsSQL Joins Defining Join Types: Non-equijoinDefining Join Types: Non-equijoin

Oracle Example:Oracle Example:SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a, table2 bFROM table1 a, table2 b

WHERE a.cola BETWEEN b.colx AND b.coly;WHERE a.cola BETWEEN b.colx AND b.coly;

Page 15: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 1515

SQL JoinsSQL Joins Defining Join Types: Non-equijoinDefining Join Types: Non-equijoin

ANSI SQL Example:ANSI SQL Example:SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a CROSS JOIN table2 bFROM table1 a CROSS JOIN table2 b

WHERE a.columnpk >= b.columnfk;WHERE a.columnpk >= b.columnfk;

Page 16: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 1616

SQL JoinsSQL Joins Defining Join Types: Non-equijoinDefining Join Types: Non-equijoin

ANSI SQL Example:ANSI SQL Example:SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a CROSS JOIN table2 bFROM table1 a CROSS JOIN table2 b

WHERE a.column BETWEEN b.colx AND b.coly;WHERE a.column BETWEEN b.colx AND b.coly;

Page 17: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 1717

SQL JoinsSQL Joins

Defining Join Types: Outer Defining Join Types: Outer JoinJoin

A B

RIGHT JOIN

A B

LEFT JOIN

Page 18: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 1818

SQL JoinsSQL Joins

Defining Join Types: Outer Defining Join Types: Outer JoinJoin

A match that includes all the A match that includes all the matches between two copies of one matches between two copies of one table or two tables, andtable or two tables, and

All the non-matches from one copy of All the non-matches from one copy of the single table, or one table of the the single table, or one table of the two tables.two tables.

Page 19: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 1919

SQL JoinsSQL Joins

Defining Join Types: Outer Defining Join Types: Outer JoinJoin

Oracle Syntax:Oracle Syntax: The The “(+)”“(+)” symbol is used to create an symbol is used to create an OUTER JOINOUTER JOIN..

When the When the “(+)”“(+)” symbol is on the right symbol is on the right of the join operand, it acts as the of the join operand, it acts as the equivalent of a equivalent of a LEFT JOINLEFT JOIN..

When the When the “(+)”“(+)” it is on the left of the it is on the left of the join operand, it is the equivalent of a join operand, it is the equivalent of a RIGHT JOINRIGHT JOIN. .

Page 20: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 2020

SQL JoinsSQL Joins

Defining Join Types: Outer Defining Join Types: Outer JoinJoin

ANSI Syntax:ANSI Syntax: These are defined by These are defined by LEFT JOINLEFT JOIN and and RIGHT JOINRIGHT JOIN

operators.operators. Both Both LEFT [OUTER] JOINLEFT [OUTER] JOIN and and RIGHT [OUTER] JOINRIGHT [OUTER] JOIN are are

synonymous with synonymous with LEFT JOINLEFT JOIN and and RIGHT JOINRIGHT JOIN respectively, respectively, the the OUTEROUTER is assumed when left out. is assumed when left out.

The The OUTEROUTER keyword is optional or implicitly derived by the keyword is optional or implicitly derived by the SQL parser.SQL parser.

These join semantics are unidirectional outer joins and These join semantics are unidirectional outer joins and synonymous or alike behaviors because the only difference synonymous or alike behaviors because the only difference is the direction as to how their behavior is applied.is the direction as to how their behavior is applied.

The The LEFT [OUTER] JOINLEFT [OUTER] JOIN finds the non-matching values on finds the non-matching values on the right-side of the operand.the right-side of the operand.

The The RIGHT [OUTER] JOINRIGHT [OUTER] JOIN finds the non-matching values finds the non-matching values on the left-side of the operand.on the left-side of the operand.

Page 21: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 2121

SQL JoinsSQL Joins

Defining Join Types: Outer Defining Join Types: Outer JoinJoin

Oracle Example (left join):Oracle Example (left join):SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a, table2 bFROM table1 a, table2 b

WHERE a.columnpk = b.columnfk(+);WHERE a.columnpk = b.columnfk(+);

Page 22: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 2222

SQL JoinsSQL Joins

Defining Join Types: Outer Defining Join Types: Outer JoinJoin

Oracle Example (right join):Oracle Example (right join):SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a, table2 bFROM table1 a, table2 b

WHERE a.columnpk(+) = b.columnfk;WHERE a.columnpk(+) = b.columnfk;

Page 23: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 2323

SQL JoinsSQL Joins

Defining Join Types: Outer Defining Join Types: Outer JoinJoin

ANSI SQL Example:ANSI SQL Example:SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a LEFT JOIN table2 bFROM table1 a LEFT JOIN table2 b

ON a.columnpk = b.columnfk;ON a.columnpk = b.columnfk;

Page 24: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 2424

SQL JoinsSQL Joins

Defining Join Types: Outer Defining Join Types: Outer JoinJoin

ANSI SQL Example:ANSI SQL Example:SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a RIGHT JOIN table2 bFROM table1 a RIGHT JOIN table2 b

ON a.columnpk = b.columnfk;ON a.columnpk = b.columnfk;

Page 25: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 2525

SQL JoinsSQL Joins Defining Join Types: Full Outer JoinDefining Join Types: Full Outer Join

A match that includes all matches A match that includes all matches between two tables plus all non-matches between two tables plus all non-matches whether on the left or right side of a join.whether on the left or right side of a join.

Oracle syntax: There is no full outer join Oracle syntax: There is no full outer join syntax.syntax.

Oracle syntax: The Oracle syntax: The UNIONUNION operator to operator to mimic the behavior.mimic the behavior.

ANSI SQL synatx: Uses the ANSI SQL synatx: Uses the FULL [OUTER] FULL [OUTER] JOINJOIN operator. operator.

Page 26: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 2626

SQL JoinsSQL Joins

Defining Join Types: Outer Defining Join Types: Outer JoinJoin

Oracle Example:Oracle Example:SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a, table2 bFROM table1 a, table2 b

WHERE a.columnpk(+) = b.columnfkWHERE a.columnpk(+) = b.columnfk

UNIONUNION

SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a, table2 bFROM table1 a, table2 b

WHERE a.columnpk = b.columnfk(+);WHERE a.columnpk = b.columnfk(+);

Page 27: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 2727

SQL JoinsSQL Joins

Defining Join Types: Full JoinDefining Join Types: Full Join ANSI SQL Example:ANSI SQL Example:

SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM table1 a FULL [OUTER] JOIN table2 bFROM table1 a FULL [OUTER] JOIN table2 b

ON a.columnpk = b.columnfk;ON a.columnpk = b.columnfk;

Page 28: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 2828

SQL JoinsSQL Joins

Defining Join Types: Self JoinDefining Join Types: Self Join A recursive join internally within a A recursive join internally within a

single table based on a primary and single table based on a primary and foreign key residing in each row of foreign key residing in each row of data in a table.data in a table.

You must use table name aliases to You must use table name aliases to create a create a SELF JOINSELF JOIN..

Self joins typically use two separate Self joins typically use two separate column names.column names.

Page 29: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 2929

SQL JoinsSQL Joins

Defining Join Types: Self JoinDefining Join Types: Self Join Oracle Example:Oracle Example:

SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM FROM table1table1 a, a, table1table1 b b

WHERE a.columnpk = b.columnfk;WHERE a.columnpk = b.columnfk;

Page 30: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 3030

SQL JoinsSQL Joins

Defining Join Types: Self JoinDefining Join Types: Self Join ANSI SQL Example:ANSI SQL Example:

SELECT a.column1, b.column2SELECT a.column1, b.column2

FROM FROM table1table1 a [INNER] JOIN a [INNER] JOIN table1table1 b b

ON a.columnpk = b.columnfk;ON a.columnpk = b.columnfk;

Page 31: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 3131

SQL JoinsSQL Joins

Defining Join Types: Defining Join Types: InheritanceInheritance

INNER JOIN

FULL OUTER JOIN

OUTER JOIN

LEFT JOIN RIGHT JOIN

NATURAL JOIN

Page 32: SQL Joins Oracle and ANSI Standard SQL

Copyright 2006Copyright 2006 Page Page 3232

SummarySummary Inner join or Inner join or equijoinequijoin

Surrogate keysSurrogate keys Natural keysNatural keys

Non-equijoinNon-equijoin Outer joinOuter join Full outer joinFull outer join Self joinSelf join Join behavior inheritance Join behavior inheritance

treetree