Welcome
TOPIC
SQL JOINS
CONTENT
INTRODUCTION
WHAT IS JOINNING???
DEFINITION
A SQL join clause combines columns from
one or more tables in a relational database.
It creates a set that can be saved as a table
or used as it is.
A JOIN is a means for combining columns
from one (self-table) or more tables by using
values common to each.
TYPES
SQL JOINS
Inner Join
Outer Join
Cross Join
INNER JOINS
I.INNER JOIN
Inner join returns only those records that match in both the tables.
Syntax:
Select * from table 1 INNER JOIN
table 2
ON
Table 1.Column 1=table 2.Column 1
SCENARIO
INNER JOINS
QUARY:
SELECT foods.item_name,foods.item_unit,
company.company_name,company.company_city FROM foods
INNER JOIN company
ON foods.company_id =company.company_id;
Result:
ITEM_NAME ITEM_ COMPANY_NAMECOMPANY_CITY
Chex Mix Pcs Akas Foods Delhi
Cheez-It Pcs Jack Hill Ltd London
BN Biscuit Pcs Jack Hill Ltd London
Mighty Munch Pcs Foodies. London
Pot Rice Pcs Jack Hill Ltd London
Jaffa Cakes Pcs Order All Boston
RIGHT JOINS
a. Right Outer Join
Right outer join returns all records/rows from right table and from left table
returns only matched records.
Syntax:
Select * from Table 1
Right Outer Join Table 2
ON
Table 1.Column 1=Table 2.Column 1
LEFT JOINS
b. Left Outer Join
Left outer join returns all records/rows from left table and from right
table returns only matched records.
Syntax:
Select * from Table 1
Left Outer Join Table 2
ON
Table 1.Column 1=Table 2.Column
1
SCENARIO
RIGHT JOINS
QUARY:
SELECT company.company_id,company.company_name,
company.company_city,foods.company_id,foods.item_name
FROM company
RIGHT JOIN foods
ON company.company_id = foods.company_id;
RESULT:
COMPANY_ID COMPANY_NAME COMPANY_CITY COMPANY_ID ITEM_NAME
18 Order All Boston 18 Jaffa Cakes
15 Jack Hill Ltd London 15 Pot Rice
15 Jack Hill Ltd London 15 BN Biscuit
15 Jack Hill Ltd London 15 Cheez-It
16 Akas Foods Delhi 16 Chex Mix
17 Foodies. London 17 Mighty Munch
Salt n Shake
LEFT JOINS
QUARY:
SELECT company.company_id,company.company_name,
company.company_city,foods.company_id,foods.item_name
FROM company
LEFT JOIN foods
ON company.company_id = foods.company_id;
RESULT:
COMPANY_ID COMPANY_NAME COMPANY_CITY COMPANY_ID ITEM_NAME
16 Akas Foods Delhi 16 Chex Mix
15 Jack Hill Ltd London 15 Cheez-It
15 Jack Hill Ltd London 15 BN Biscuit
17 Foodies. London 17 Mighty
15 Jack Hill Ltd London 15 Pot Rice
18 Order All Boston 18 Jaffa Cakes
19 sip-n-Bite. New York
FULL JOINS
c. Full Outer Join
Full outer join combines left outer join and right outer join. This join
returns all records/rows from both the tables.
Syntax:
Select * from Table 1
Full Outer Join Table 2
ON
Table 1.Column 1=Table 2.Column
1
CROSS JOINS
III. Cross Join
This join returns records/rows that are multiplication of record number
from both the tables means each row on left table will related to each row
of right table.
Syntax:
Select * from Table 1
Cross Join Table 2
SCENARIO
QUARY:
FULL JOINS
SELECT a.company_id AS "a.ComID",
a.company_name AS "C_Name", b.company_id AS
"b.ComID",
b.item_name AS "I_Name"FROM company a
FULL OUTER JOIN foods b
ON a.company_id = b.company_id;
RESULT:
a.ComID C_Name b.ComID I_Name
16 Akas Foods 16 Chex Mix
15 Jack Hill Ltd 15 Cheez-It
15 Jack Hill Ltd 15 BN Biscuit
17 Foodies. 17 Mighty Munch
15 Jack Hill Ltd 15 Pot Rice
18 Order All 18 Jaffa Cakes
Salt n Shake
19 sip-n-Bite.
CROSS JOINS
QUARY:
SELECT foods.item_name,foods.item_unit,
company.company_name,company.company_city
FROM foods
CROSS JOIN company;
RESULT: