Anda di halaman 1dari 44

JOINS

• Sometimes you need data from more than


one table. In such cases you use joins.
Types of joins
• Self join
• Inner join
• Natural join
• Outer join
– Left outer join
– Right outer join
– Full outer join
• Cross join
• Equijoins ( when a condition with ‘=‘ is used)
• Non-Equijoins (when a condition with <,>,!= etc is
used)
• The LEFT OUTER JOIN keyword returns all the
rows from the left table (Customers), even if
there are no matches in the right table
(Orders).
Right Outer Join
• The RIGHT OUTER JOIN keyword returns all rows
from the right table (table2), with the matching
rows in the left table (table1). The result is NULL
in the left side when there is no match.
• SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
Example
• SELECT Orders.OrderID, Employees.FirstName
FROM Orders
RIGHT OUTER JOIN Employees
ON
Orders.EmployeeID=Employees.EmployeeID
ORDER BY Orders.OrderID;
Full Outer Join
• The FULL OUTER JOIN keyword returns all
rows from the left table (table1) and from the
right table (table2).
• The FULL OUTER JOIN keyword combines the
result of both LEFT and RIGHT joins.
Example
• SELECT Customers.CustomerName,
Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON
Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
• The FULL OUTER JOIN keyword returns all the
rows from the left table (Customers), and all
the rows from the right table (Orders). If there
are rows in "Customers" that do not have
matches in "Orders", or if there are rows in
"Orders" that do not have matches in
"Customers", those rows will be listed as well.
Self joins AND cross joins
• Joining a table to itself.
• In self-join two rows of the same table
combine to form a result row.
• Cross join gives us the Cartesian product
Quick Review
• Consider 2 tables ( employee AND boss) as
below:
• All joins are implemented as per these tables
in the following 7 slides:

EMPLOYEE BOSS
NATURAL JOIN
• select id,E_name,B_name from Employee
natural join Boss where
employee.E_name>'N';
• In the output, only matching record which had
a match in the matching column(id) and
E_name is greater than N is shown in the
output.
SELF JOIN
• select a.e_name,a.id,b.manager_id from
employee a, employee b where b.manager_id
= a.id;
• The table employee is seen as two different
tables i.e table a and table b by oracle. And
then, table a and b are joined together.
• Below is the output for the above query
CROSS JOIN
• select employee.e_name,boss.b_name from
employee cross join boss;
• A total of 16 rows are shown in
the output.
• Each Record of employee table
is joined with all the records
of the boss table.
INNER JOIN
• select employee.e_name,boss.b_name from
employee inner join boss on
employee.id=boss.id;
• Condition is given with the help of ‘on’ clause
in inner join.
• If id in employee table is equal to id in boss
table, then it is listed in the output.
LEFT OUTER JOIN
• select employee.e_name,boss.b_name from
employee left outer join boss on
employee.id=boss.id;
• All the records of employee table and
matching records of boss table are listed.
RIGHT OUTER JOIN
• select employee.e_name,boss.b_name from
employee right outer join boss on
employee.id=boss.id;
• All the rows of boss table and matching rows
of employee table are listed
FULL OUTER JOIN
• select employee.e_name,boss.b_name from
employee full outer join boss on
employee.id=boss.id;
• Both the tables are fully listed
UNDERSTANDING JOINS
SELF JOINS
Non equi joins
Cross Join
• When all the records of table 1 are combined
with al the records of table 2

Anda mungkin juga menyukai