EXPERIMENT NO: 09
TITLE: To Perform different types of joins in Oracle.
Aim: To Perform different types of joins in Oracle.
Requirements : Oracle 10g
Theory:
Definition of Join
The join keyword is used in sql statement to query data from multiple tables.
Data from Multiple Tables
Sometimes you need to use data from more than one table. In the slide example, the report
displays
data from two separate tables.
• Employee IDs exist in the EMPLOYEES table.
• Department IDs exist in both the EMPLOYEES and DEPARTMENTS tables.
• Location IDs exist in the DEPARTMENTS table.
To produce the report, you need to link the EMPLOYEES and DEPARTMENTS tables and
access data from both of them.
SQL: 1999 Compliant Joins:
• Cross joins
• Natural join
• Left outer join
• Right outer join
• Full Outer join
1) Cross join:
• The CROSS JOIN clause produces the cross product of two tables.
• This is the same as a Cartesian product between the two tables.
Syntax
select column names
from table_name1
Cross join table_name2;
Example:
2) Natural join:
• The NATURAL JOIN clause is based on all columns in the two tables that have the same
name.
• It selects rows from the two tables that have equal values in all matched columns.
• If the columns having the same names have different data types, an error is returned.
Creating Natural Joins
It was not possible to do a join without explicitly specifying the columns in the corresponding
tables in
prior releases of Oracle. In Oracle9i it is possible to let the join be completed automatically
based on
columns in the two tables which have matching data types and names, using the keywords
NATURAL JOIN keywords.
Note: The join can happen only on columns having the same names and data types in both the
tables.If the columns have the same name, but different data types, then the NATURAL JOIN
syntax causes an error.
Syntax:
Select column1, column2
From table1
natural join table2;
Example:
3) Left outer join:
This query retrieves all rows from left table even if there is no match in the right table
Syntax:
Select table1.column, table2.column
From table1
Left outer join table2
On (table1.column=table2.column);
Example:
4) Right outer join:
This query retrieves all rows from right table, even if there is no match in the left table.
Syntax:
Select table1.column, table2.column
From table1
Right outer join table2
On (table1.column = table2.column);
Example:
5) Full outer join:
This query retrieves all rows in the left table, even if there is no match in the
Right table. It also retrieves all rows in the right table, even if there is no match
In the left table.
Syntax:
Select table1.column, table2.column
from table1
Full outer join table2
On (table1.column = table2.column)
Example:
Conclusion:
Questions:
1. What is Inner join?
2. Explain full outer join?
3. Explain left outer join with example?
4. Explain equijoin operation?
5. Explain cross join with example?
6. Write syntax for natural join.
7. Explain right outer join with example.