6. 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.
DEFINITION
8. 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
10. INNER JOINS
Result:
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;
ITEM_NAME ITEM_ COMPANY_NAME COMPANY_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
11. 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
12. 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
14. 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
15. LEFT JOINS
RESULT:
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;
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
16. FULL JOINSc. 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
17. 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