0% found this document useful (0 votes)
70 views9 pages

Displaying Data From Mutiple Table: Joins

This document discusses different types of joins in SQL and Oracle for retrieving data from multiple tables. It describes inner joins, outer joins (left, right, full), natural joins, joins using the USING clause, and cross joins. The key types of joins are inner joins which return matched rows, outer joins which return matched rows and unmatched rows from one table, and cross joins which perform a Cartesian product between tables.

Uploaded by

Dhmi yassin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views9 pages

Displaying Data From Mutiple Table: Joins

This document discusses different types of joins in SQL and Oracle for retrieving data from multiple tables. It describes inner joins, outer joins (left, right, full), natural joins, joins using the USING clause, and cross joins. The key types of joins are inner joins which return matched rows, outer joins which return matched rows and unmatched rows from one table, and cross joins which perform a Cartesian product between tables.

Uploaded by

Dhmi yassin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

MODULE OF INSTRUCTION

Displaying Data from Mutiple Table

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.

Different type of SQL and Oracle Compliant joins, adding WHERE in


condition and sorting data from one or more 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:

 Oracle INNER JOIN (or sometimes called simple join)


 Oracle LEFT OUTER JOIN (or sometimes called LEFT JOIN)
 Oracle RIGHT OUTER JOIN (or sometimes called RIGHT
JOIN)
 Oracle FULL OUTER JOIN (or sometimes called FULL JOIN)

While other SQL Compliant Joins are the following:

 Natural Join
 Using
 Cross Join

Whenever a join is omitted a Cartesian Product is generated, this result


to a product of 1 or more table.

After completing this lesson, the student should be able to:

 Lists the different types of Join Condition.

 Use SELECT statement to display data from multiple


table with different type of JOIN condition.

 Compare and Contrast Inner and Outer Join.

Database Management System 2 1


Week 10 Displaying data from Mulitple table

 Explain and Discuss why Cartesian product is


encountered.

Types of Joins

Type of Joins that are compliant with the SQL:1999 standard include
the following:

1. Natural join with the NATURAL JOIN clause

2. Join with the USING Clause

3. OUTER joins:

• LEFT OUTER JOIN

• RIGHT OUTER JOIN

• FULL OUTER JOIN

4. Join with the ON Clause

5. Cross join with the CROSS JOIN clause

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

Qualifying Ambiguous Column Names

 To quality ambiguous columns always use table prefixes.

2
MODULE OF INSTRUCTION

 Table prefixes is used to improve performance.

 If table prefixes is too long, a user may use table aliases.

 Table alias gives a table a shorter name:

 Keeps SQL code smaller, uses less memory

 Use column aliases to distinguish columns that have identical


names, but reside in different tables.

Creating Natural Joins

 The NATURAL JOIN clause is based on all the 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 but with different data
types, an error is returned.

 Syntax:
SELECT COLUMN_LIST(S)
FROM TABLE 1
NATURAL JOIN TABLE 2;

Creating Joins with the USING Clause

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

 The NATURAL JOIN and USING clauses are mutually


exclusive.

 Syntax:
SELECT COLUMN_LIST(S)
FROM TABLE 1 JOIN TABLE 2
USING (SAME_COLUMN_NAME);

Database Management System 2 3


Week 10 Displaying data from Mulitple table

Using Table Aliases with the USING Clause

 You can use table alias if the column that is used in the USING
clause do not qualify.

 Note that If the same column is used elsewhere in the SQL


statement, do not alias it.

Creating Joins with the ON Clause

 The join condition for the natural join is basically an equijoin


of all columns with the same name.

 Use the ON clause to specify arbitrary conditions or specify


columns to join.

 The join condition is separated from other search conditions.

 The ON clause makes code easy to understand.

 Syntax:
SELECT COLUMN_LIST(S)
FROM TABLE 1 JOIN TABLE 2
ON(TABLE1.SAME_COLUMN_NAME = TABLE2.
SAME_COLUMN_NAME);

Applying Additional Conditions to a Join

 Use the AND clause or the WHERE clause to apply additional


conditions.

 To limit the retuned by the query use the Logical and Where
condition.

Inner Join

• Inner join and On clause returns the same output.

4
MODULE OF INSTRUCTION

• Inner Join – returns the result of intersection of two or more


table.

• Syntax:
SELECT COLUMN_LIST(S)
FROM TABLE 1 INNER JOIN TABLE 2
ON(TABLE1.SAME_COLUMN_NAME = TABLE2.
SAME_COLUMN_NAME);

INNER Versus OUTER Joins

 In SQL:1999, the join of two tables returning only matched


rows is called an INNER join.

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

 A join between two tables that returns the results of an INNER


join as well as the results of a left and right join is a full
OUTER join

Left Join

 Is a join of two table prioritizing the values from the Left


Table.

 Syntax:
SELECT COLUMN_LIST(S)
FROM TABLE 1 LEFT OUTER JOIN TABLE 2
ON(TABLE1.SAME_COLUMN_NAME = TABLE2.
SAME_COLUMN_NAME);

Database Management System 2 5


Week 10 Displaying data from Mulitple table

Right Join

 Is a join of two table prioritizing the values from the Right


Table.
SELECT COLUMN_LIST(S)
 Syntax:
FROM TABLE 1 FULL OUTER JOIN TABLE 2
ON(TABLE1.SAME_COLUMN_NAME = TABLE2. SAME_COLUMN_NAME);

Full Outer Join

 Is a join of two table either left or right outer 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

 A Cartesian product is formed when:

 A join condition is omitted

 A join condition is invalid

 All rows in the first table are joined to all rows in the
second table

 Always include a valid join condition if you want to avoid a


Cartesian product.

Cross Join

 The CROSS JOIN clause produces the cross-product of two


tables.

 This is also called a Cartesian product between the two tables.

LESSON SUMMARY:

SQL complaint join is composed of the following:


 Natural join is used when the two table joined together
have the same values
 Using Clause is used when the two joined table
columns with the same values are off different data
type.
While Oracle Compliant Joins are the following.
 Equijoin is a join condition used when two joined table
have column with the same values. This is used using
the ON Clause.
 To get values / rows that are said to outer join of two
table a user can choose with RIGHT/LEFT or FULL
outer join.
 Right outer join is used when the desired output will
priority values of table from the right.
 Left outer join is used when the desired output will
priority values of table from the left.
 Full outer join is used when the desired output will
priority all non-equijoin values on both table.

Database Management System 2 7


Week 10 Displaying data from Mulitple table

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

 Inner Join: returns the result of intersection of two or more


table.

 Left Join: Is a join of two table prioritizing the values from the
Left Table.

 Natural Join: clause is based on all the columns in the two


tables that have the same name.

 ON: use to specify arbitrary conditions or specify columns to


join.

 Right Join: Is a join of two table prioritizing the values from


the Right Table.

 Table Alias: Gives a table shoter name

8
MODULE OF INSTRUCTION

 Table Prefixes: used to qualitfy ambigour columns.

 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

Other Suggested Readings (e.g. periodicals, articles, websites, IT


applications/software, etc.):
 www.oracle.com
 www.apex.oracle.com
 SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
 SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/S
QL
 Database Administration. In Encyclopedia.com,
Retrieved from
http://www.encyclopedia.com/topic/Database_administ
ration.aspx
 SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspxLearning
Icons

Database Management System 2 9

You might also like