Displaying Data From Mutiple Table: Joins
Displaying Data From Mutiple Table: Joins
Welcome to the sixth module of this course. This lesson explains how
to get data from more than one table. A join is used to view
information from multiple tables. Therefore, you can join tables
together to view information from more than one table.
Joins
Oracle JOINS are used to retrieve data from multiple tables. An Oracle
JOIN is performed whenever two or more tables are joined in a SQL
statement.
In Oracle There are 4 different types of Oracle joins:
Natural Join
Using
Cross Join
Types of Joins
Type of Joins that are compliant with the SQL:1999 standard include
the following:
3. OUTER joins:
Syntax
SELECT table1.column_name,
table1.column_name,table2.column_name,
table2.column_name
FROM table 1
[NATURAL JOIN table 2]
[CROSS JOIN table 2]
[JOIN table 2 USING (column_name)]
[JOIN table 2 ON table1.column_name = table2.column_name]
[LEFT/RIGHT/FULL OUTER JOIN table 2
ON table1.column_name = table2.column_name]
[WHERE/AND/OR/NOT condition]
[ORDER BY DESC/ASC];
2
MODULE OF INSTRUCTION
It selects rows from the two tables that have equal values in all
matched columns.
If the columns having the same names but with different data
types, an error is returned.
Syntax:
SELECT COLUMN_LIST(S)
FROM TABLE 1
NATURAL JOIN TABLE 2;
If several columns have the same names but the data types do
not match, use the USING clause to specify the columns for the
equijoin.
Use the USING clause to match only one column when more
than one column matches.
Syntax:
SELECT COLUMN_LIST(S)
FROM TABLE 1 JOIN TABLE 2
USING (SAME_COLUMN_NAME);
You can use table alias if the column that is used in the USING
clause do not qualify.
Syntax:
SELECT COLUMN_LIST(S)
FROM TABLE 1 JOIN TABLE 2
ON(TABLE1.SAME_COLUMN_NAME = TABLE2.
SAME_COLUMN_NAME);
To limit the retuned by the query use the Logical and Where
condition.
Inner Join
4
MODULE OF INSTRUCTION
• Syntax:
SELECT COLUMN_LIST(S)
FROM TABLE 1 INNER JOIN TABLE 2
ON(TABLE1.SAME_COLUMN_NAME = TABLE2.
SAME_COLUMN_NAME);
A join between two tables that returns the results of the INNER
join as well as the unmatched rows from the left (or right) table
is called a left (or right) OUTER join.
Left Join
Syntax:
SELECT COLUMN_LIST(S)
FROM TABLE 1 LEFT OUTER JOIN TABLE 2
ON(TABLE1.SAME_COLUMN_NAME = TABLE2.
SAME_COLUMN_NAME);
Right Join
Syntax:
SELECT COLUMN_LIST(S)
FROM TABLE 1 RIGHT OUTER JOIN TABLE 2
ON(TABLE1.SAME_COLUMN_NAME = TABLE2. SAME_COLUMN_NAME);
6
MODULE OF INSTRUCTION
Cartesian Products
All rows in the first table are joined to all rows in the
second table
Cross Join
LESSON SUMMARY:
When the user omit the join condition the put is called a Cartesian
product for oracle while in other SQL complainant join it is called as
cross join.
Activities/Exercises
Download the Laboratory Exercise 5: Mulitple Table:
Joins
Copy and paste the three (3) table EMPLOYEES,
DEPARTMENTS and LOCATIONS
Perform the PL/SQL needed per number in order for
you to answer the questions in eacg number.
Write the excat answer
Glossary
CROSS JOIN clause: produces the cross-product of two
tables.
Full outer join: Is a join of two table either left or right outer
join
Left Join: Is a join of two table prioritizing the values from the
Left Table.
8
MODULE OF INSTRUCTION
USING: used to match only one column when more than one
column matches.
References
Textbook:
Oracle Database 11g 2nd Edition K Gopalakrishnan (
2012)
References:
Carlos, Peter (2009). Database Systems
Connoly, Thomas & Begg, Carolyn (2010). Database
Systems : A practical approach to design,
implementation and management
Sciore, Edward (2009). Database Design and
Implementation
Bulusu, Lakshman (2008). Oracle PL/SQL : Expert
Techniques for Developers and Database
Administrators
Loshin, David (2008). Master Data Management