Beruflich Dokumente
Kultur Dokumente
Rajalakshumi K
1 Introduction
2 SQL Joins
3 Types of Join
5 Conclusion
2 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Introduction
David 20 40 Marketing
Joy Null
4 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Types of Join
5 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Types of Join
• Inner Join
Equi Join
Natural Join
Cross Join
• Outer Join
Left Outer Join
Right Outer Join
Full Outer Join
• Self Join
6 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Inner Join
An Inner Join is the default join type and it is used widely in most applications .It
creates a new result table by combining column values of two tables (A and B) based
upon the join-predicate .
SQL specifies two different ways to express joins: "explicit join notation" and
"implicit join notation".
Example:
Explicit join notation:
SELECT *
FROM employee
INNER JOIN department
ON employee.deptno= department.deptno
Implicit join notation:
SELECT *
FROM employee, department
Where employee.deptno= department.deptno
7 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Results of the Implicit query:
Employee.EmPNa Employee.DeptNo Department.DeptN Department.De
me ame ptNo
Rahul 30 Finance 30
Raji 10 Sales 10
David 20 Engineering 20
Roshan 30 Finance 30
8 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Natural Join(Natural Join is a Specialized Employee. Employee. Department
form of Equi join ) EmPName DeptNo .DeptName(
?)
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
Rahul 30 Finance
have equal values in all matched
columns.
If the columns having the same names Raji 10 Sales
have different data types, an error is
returned.
Retrieving Records with Natural Joins
David 20 Engineering
SELECT empname,deptname,deptno
Roshan 30 Finance
FROM employee
NATURAL JOIN department
9 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Cross Join(Cartesian product) Employee.EmPNa Department.Dept
Cross Join is Cartesian product which me Name
provides the ground work for all the
inner joins
This is the same as a Cartesian Rahul Finance
product between the two tables.
Retrieving Records with Cross Joins
Joy null
SELECT empname,deptname
FROM employee
CROSS JOIN department Raji Sales
David Engineering
Roshan Finance
10 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Outer Join
A join between two tables that returns the results of the inner join as well as
unmatched rows left (or right) tables is 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.
Retrieving Records with Left Outer Joins
SELECT *
FROM employee
LEFT OUTER JOIN department
ON employee.DeptNo = department.DeptNo
Retrieving Records with Right Outer Joins
SELECT *
FROM employee
RIGHT OUTER JOIN department
ON employee.DeptNo = department.DeptNo
Retrieving Records with Full Outer Joins
SELECT *
FROM employee
FULL OUTER JOIN department
ON employee.DeptNo = department.DeptNo
11 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Results of Outer Join.
Left Outer Join
Employee.EmPN Employee. Department.De Department.D
ame DeptNo ptName eptNo
Rahul 30 Finance 30
Raji 10 Sales 10
David 20 Engineering 20
Roshan 30 Finance 30
12 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Right Outer Join:
Employee.EmP Employee.Dept Department.De Department.De
Name No ptName ptNo
Rahul 30 Finance 30
Raji 10 Sales 10
David 20 Engineering 20
Roshan 30 Finance 30
13 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Full Outer Join
Employee.EmPName Employee.Dep Department.D Department.D
tNo eptName eptNo
Rahul 30 Finance 30
Raji 10 Sales 10
Roshan 30 Finance 30
David 20 Engineering 20
Join Order:
A row source is a logical set of rows from a partially complete query. The row
source may be rows in an actual table or rows in a temporary segment stored on
disk or in memory. Oracle always joins two row sources at a time. Therefore, if a
SQL statement joins three tables, Oracle will actually join two tables together and
then join the results with the third table.
The order in which row sources are joined can have a significant impact on
performance. The optimizer makes a list of all physically possible join orders and
then chooses what it believes will be the best option. The cost-based optimizer
makes this decision by estimating the cost of each option and choosing the one with
the lowest cost. The rule-based optimizer, meanwhile, scores each option based on a
fixed set of rules and chooses the option with the best score.
Outer joins limit the number of possible join orders because a table being outer
joined must appear in the join order after the table it joins to.
15 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Conclusion:
Hence Join plays a vital role in database to (manipulate and access data) view or
creating data accordingly. The Oracle optimizer needn’t be explicitly given what join
methods to use or the order in which to join tables when executing the query, but
understanding how Oracle processes joins will help in designing better database
schemas and write more efficient SQL. The better the schema design, the less tuning
of individual statements will be necessary. When the schema, individual SQL, and the
database as a whole have been tuned for joins, applications will run better all
around.
16 ©
© 2010
2009 Wipro
Wipro Ltd
Ltd -- Confidential
Confidential
Thank You
Rajalakshumi K
rajalakshumi.kra@wipro.com