0% found this document useful (0 votes)
10 views

What Is SQL Joins

SQL joins combine rows from two or more tables by using values common between them. There are two main types of joins - equi joins which use equality comparisons and non-equi joins which use other comparisons. Equi joins include inner and outer joins, while non-equi joins use comparisons like greater than.

Uploaded by

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

What Is SQL Joins

SQL joins combine rows from two or more tables by using values common between them. There are two main types of joins - equi joins which use equality comparisons and non-equi joins which use other comparisons. Equi joins include inner and outer joins, while non-equi joins use comparisons like greater than.

Uploaded by

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

What is SQL Joins?

An SQL JOIN clause combines rows from two or more tables. It


creates a set of rows in a temporary table.

How to Join two tables in SQL?


A JOIN works on two or more tables if they have at least one
common field and have a relationship between them.

JOIN keeps the base tables (structure and data) unchanged.

SQL JOINS: EQUI JOIN and NON EQUI JOIN


The are two types of SQL JOINS - EQUI JOIN and NON EQUI
JOIN

1) SQL EQUI JOIN :

The SQL EQUI JOIN is a simple SQL join uses the equal sign(=)
as the comparison operator for the condition. It has two types -
SQL Outer join and SQL Inner join.

2) SQL NON EQUI JOIN :

The SQL NON EQUI JOIN is a join uses comparison operator


other than the equal sign like >, <, >=, <= with the condition.

SQL EQUI JOIN : INNER JOIN and OUTER JOIN


The SQL EQUI JOIN can be classified into two types - INNER
JOIN and OUTER JOIN

1. SQL INNER JOIN

This type of EQUI JOIN returns all rows from tables where the
key record of one table is equal to the key records of another
table.

2. SQL OUTER JOIN

This type of EQUI JOIN returns all rows from one table and
only those rows from the secondary table where the joined
condition is satisfying i.e. the columns are equal in both
tables.
In order to perform a JOIN query, the required information we
need are:

a) The name of the tables


b) Name of the columns of two or more tables, based on which
a condition will perform.

Syntax:
FROM table1
join_type table2
[ON (join_condition)]

Parameters:

Name Description

table1, table2 Tables participating in joining.

join_type Type of the join.

join_condition Some condition. This is optional.


Pictorial Presentation of SQL Joins:

Example:

Sample table: company


+------------+---------------+--------------+
| COMPANY_ID | COMPANY_NAME | COMPANY_CITY |
+------------+---------------+--------------+
| 18 | Order All | Boston |
| 15 | Jack Hill Ltd | London |
| 16 | Akas Foods | Delhi |
| 17 | Foodies. | London |
| 19 | sip-n-Bite. | New York |
+------------+---------------+--------------+
Sample table: foods
+---------+--------------+-----------+------------+
| ITEM_ID | ITEM_NAME | ITEM_UNIT | COMPANY_ID |
+---------+--------------+-----------+------------+
| 1 | Chex Mix | Pcs | 16 |
| 6 | Cheez-It | Pcs | 15 |
| 2 | BN Biscuit | Pcs | 15 |
| 3 | Mighty Munch | Pcs | 17 |
| 4 | Pot Rice | Pcs | 15 |
| 5 | Jaffa Cakes | Pcs | 18 |
| 7 | Salt n Shake | Pcs | |
+---------+--------------+-----------+------------+

To join two tables 'company' and 'foods', the following SQL


statement can be used :

SQL Code:
SELECT company.company_id,company.company_name,

foods.item_id,foods.item_name

FROM company,foods;

Output:
COMPAN COMPANY_NAME ITEM_ID ITEM_NAME
------ ------------------------- -------- ---------------
18 Order All 1 Chex Mix
18 Order All 6 Cheez-It
18 Order All 2 BN Biscuit
18 Order All 3 Mighty Munch
18 Order All 4 Pot Rice
18 Order All 5 Jaffa Cakes
18 Order All 7 Salt n Shake
15 Jack Hill Ltd 1 Chex Mix
15 Jack Hill Ltd 6 Cheez-It
15 Jack Hill Ltd 2 BN Biscuit
15 Jack Hill Ltd 3 Mighty Munch
15 Jack Hill Ltd 4 Pot Rice
15 Jack Hill Ltd 5 Jaffa Cakes
15 Jack Hill Ltd 7 Salt n Shake
16 Akas Foods 1 Chex Mix
16 Akas Foods 6 Cheez-It
16 Akas Foods 2 BN Biscuit
16 Akas Foods 3 Mighty Munch
16 Akas Foods 4 Pot Rice
16 Akas Foods 5 Jaffa Cakes
16 Akas Foods 7 Salt n Shake

What is Equi Join in SQL?


SQL EQUI JOIN performs a JOIN against equality or matching
column(s) values of the associated tables. An equal sign (=) is
used as comparison operator in the where clause to refer
equality.

You may also perform EQUI JOIN by using JOIN keyword


followed by ON keyword and then specifying names of the
columns along with their associated tables to check equality.

Syntax:
SELECT column_list
FROM table1, table2....
WHERE table1.column_name =
table2.column_name;
or
SELECT *
FROM table1
JOIN table2
[ON (join_condition)]
Pictorial representation:

Example:

Here is an example of Equi Join in SQL.


Sample table: agents
+------------+----------------------+--------------------+------------
+-----------------+---------+
| AGENT_CODE | AGENT_NAME | WORKING_AREA | COMMISSION |
PHONE_NO | COUNTRY |
+------------+----------------------+--------------------+------------
+-----------------+---------+
| A007 | Ramasundar | Bangalore | 0.15 |
077-25814763 | |
| A003 | Alex | London | 0.13 |
075-12458969 | |
| A008 | Alford | New York | 0.12 |
044-25874365 | |
| A011 | Ravi Kumar | Bangalore | 0.15 |
077-45625874 | |
| A010 | Santakumar | Chennai | 0.14 |
007-22388644 | |
| A012 | Lucida | San Jose | 0.12 |
044-52981425 | |
| A005 | Anderson | Brisban | 0.13 |
045-21447739 | |
| A001 | Subbarao | Bangalore | 0.14 |
077-12346674 | |
| A002 | Mukesh | Mumbai | 0.11 |
029-12358964 | |
| A006 | McDen | London | 0.15 |
078-22255588 | |
| A004 | Ivan | Torento | 0.15 |
008-22544166 | |
| A009 | Benjamin | Hampshair | 0.11 |
008-22536178 | |
+------------+----------------------+--------------------+------------
+-----------------+---------+

Sample table: customer


+-----------+-------------+-------------+--------------+--------------
+-------+-------------+-------------+-------------+---------------
+--------------+------------+
|CUST_CODE | CUST_NAME | CUST_CITY | WORKING_AREA | CUST_COUNTRY |
GRADE | OPENING_AMT | RECEIVE_AMT | PAYMENT_AMT |OUTSTANDING_AMT| PHONE_NO
| AGENT_CODE |
+-----------+-------------+-------------+--------------+--------------
+-------+-------------+-------------+-------------+---------------
+--------------+------------+
| C00013 | Holmes | London | London | UK |
2 | 6000.00 | 5000.00 | 7000.00 | 4000.00 | BBBBBBB
| A003 |
| C00001 | Micheal | New York | New York | USA |
2 | 3000.00 | 5000.00 | 2000.00 | 6000.00 | CCCCCCC
| A008 |
| C00020 | Albert | New York | New York | USA |
3 | 5000.00 | 7000.00 | 6000.00 | 6000.00 | BBBBSBB
| A008 |
| C00025 | Ravindran | Bangalore | Bangalore | India |
2 | 5000.00 | 7000.00 | 4000.00 | 8000.00 | AVAVAVA
| A011 |
| C00024 | Cook | London | London | UK |
2 | 4000.00 | 9000.00 | 7000.00 | 6000.00 | FSDDSDF
| A006 |
| C00015 | Stuart | London | London | UK |
1 | 6000.00 | 8000.00 | 3000.00 | 11000.00 | GFSGERS
| A003 |
| C00002 | Bolt | New York | New York | USA |
3 | 5000.00 | 7000.00 | 9000.00 | 3000.00 | DDNRDRH
| A008 |
| C00018 | Fleming | Brisban | Brisban | Australia |
2 | 7000.00 | 7000.00 | 9000.00 | 5000.00 | NHBGVFC
| A005 |
| C00021 | Jacks | Brisban | Brisban | Australia |
1 | 7000.00 | 7000.00 | 7000.00 | 7000.00 | WERTGDF
| A005 |
| C00019 | Yearannaidu | Chennai | Chennai | India |
1 | 8000.00 | 7000.00 | 7000.00 | 8000.00 | ZZZZBFV
| A010 |
| C00005 | Sasikant | Mumbai | Mumbai | India |
1 | 7000.00 | 11000.00 | 7000.00 | 11000.00 | 147-25896312
| A002 |
| C00007 | Ramanathan | Chennai | Chennai | India |
1 | 7000.00 | 11000.00 | 9000.00 | 9000.00 | GHRDWSD
| A010 |
| C00022 | Avinash | Mumbai | Mumbai | India |
2 | 7000.00 | 11000.00 | 9000.00 | 9000.00 | 113-12345678
| A002 |
| C00004 | Winston | Brisban | Brisban | Australia |
1 | 5000.00 | 8000.00 | 7000.00 | 6000.00 | AAAAAAA
| A005 |
| C00023 | Karl | London | London | UK |
0 | 4000.00 | 6000.00 | 7000.00 | 3000.00 | AAAABAA
| A006 |
| C00006 | Shilton | Torento | Torento | Canada |
1 | 10000.00 | 7000.00 | 6000.00 | 11000.00 | DDDDDDD
| A004 |
| C00010 | Charles | Hampshair | Hampshair | UK |
3 | 6000.00 | 4000.00 | 5000.00 | 5000.00 | MMMMMMM
| A009 |
| C00017 | Srinivas | Bangalore | Bangalore | India |
2 | 8000.00 | 4000.00 | 3000.00 | 9000.00 | AAAAAAB
| A007 |
| C00012 | Steven | San Jose | San Jose | USA |
1 | 5000.00 | 7000.00 | 9000.00 | 3000.00 | KRFYGJK
| A012 |
| C00008 | Karolina | Torento | Torento | Canada |
1 | 7000.00 | 7000.00 | 9000.00 | 5000.00 | HJKORED
| A004 |
| C00003 | Martin | Torento | Torento | Canada |
2 | 8000.00 | 7000.00 | 7000.00 | 8000.00 | MJYURFD
| A004 |
| C00009 | Ramesh | Mumbai | Mumbai | India |
3 | 8000.00 | 7000.00 | 3000.00 | 12000.00 | Phone No
| A002 |
| C00014 | Rangarappa | Bangalore | Bangalore | India |
2 | 8000.00 | 11000.00 | 7000.00 | 12000.00 | AAAATGF
| A001 |
| C00016 | Venkatpati | Bangalore | Bangalore | India |
2 | 8000.00 | 11000.00 | 7000.00 | 12000.00 | JRTVFDD
| A007 |
| C00011 | Sundariya | Chennai | Chennai | India |
3 | 7000.00 | 11000.00 | 7000.00 | 11000.00 | PPHGRTS
| A010 |
+-----------+-------------+-------------+--------------+--------------
+-------+-------------+-------------+-------------+---------------
+--------------+------------+

To get agent name column from agents table and cust name
and cust city columns from customer table after joining said
two tables with the following condition -

1. working area of agents and customer city of customer table


must be same,

the following SQL statement can be used:

SQL Code:
SELECT agents.agent_name,customer.cust_name,

customer.cust_city

FROM agents,customer

WHERE agents.working_area=customer.cust_city;

Output:
AGENT_NAME CUST_NAME
CUST_CITY
----------------------------------------
---------------------------------------- ------------
Ravi Kumar Ravindran
Bangalore
Ramasundar Ravindran
Bangalore
Subbarao Ravindran
Bangalore
Ravi Kumar Srinivas
Bangalore
Ramasundar Srinivas
Bangalore
Subbarao Srinivas
Bangalore
Ravi Kumar Rangarappa
Bangalore
Ramasundar Rangarappa
Bangalore
Subbarao Rangarappa
Bangalore
Ravi Kumar Venkatpati
Bangalore
Ramasundar Venkatpati
Bangalore
Subbarao Venkatpati
Bangalore
Anderson Fleming
Brisban
Anderson Jacks
Brisban
Anderson Winston
Brisban
Santakumar Yearannaidu
Chennai
...........
...........

What is the difference between Equi Join and Inner Join in SQL?
An equijoin is a join with a join condition containing an
equality operator. An equijoin returns only the rows that have
equivalent values for the specified columns.

An inner join is a join of two or more tables that returns only


those rows (compared using a comparison operator) that
satisfy the join condition.

Pictorial representation : EQUI JOIN Vs. INNER JOIN


What is NON EQUI JOIN in SQL?

The SQL NON EQUI JOIN uses comparison operator instead of


the equal sign like >, <, >=, <=along with conditions.

Syntax:
SELECT *
FROM table_name1, table_name2
WHERE table_name1.column [> | < | >= | <= ]
table_name2.column;
Pictorial representation:
Example:

Here is an example of non equi join in SQL between two tables

Sample table: orders


ORD_NUM ORD_AMOUNT ADVANCE_AMOUNT ORD_DATE CUST_CODE AGENT_CODE
ORD_DESCRIPTION
---------- ---------- -------------- --------- ---------------
--------------- -----------------
200114 3500 2000 15-AUG-08 C00002 A008
200122 2500 400 16-SEP-08 C00003 A004
200118 500 100 20-JUL-08 C00023 A006
200119 4000 700 16-SEP-08 C00007 A010
200121 1500 600 23-SEP-08 C00008 A004
200130 2500 400 30-JUL-08 C00025 A011
200134 4200 1800 25-SEP-08 C00004 A005
200108 4000 600 15-FEB-08 C00008 A004
200103 1500 700 15-MAY-08 C00021 A005
200105 2500 500 18-JUL-08 C00025 A011
200109 3500 800 30-JUL-08 C00011 A010
200101 3000 1000 15-JUL-08 C00001 A008
200111 1000 300 10-JUL-08 C00020 A008
200104 1500 500 13-MAR-08 C00006 A004
200106 2500 700 20-APR-08 C00005 A002
200125 2000 600 10-OCT-08 C00018 A005
200117 800 200 20-OCT-08 C00014 A001
200123 500 100 16-SEP-08 C00022 A002
200120 500 100 20-JUL-08 C00009 A002
200116 500 100 13-JUL-08 C00010 A009
200124 500 100 20-JUN-08 C00017 A007
200126 500 100 24-JUN-08 C00022 A002
200129 2500 500 20-JUL-08 C00024 A006
200127 2500 400 20-JUL-08 C00015 A003
200128 3500 1500 20-JUL-08 C00009 A002
200135 2000 800 16-SEP-08 C00007 A010
200131 900 150 26-AUG-08 C00012 A012
200133 1200 400 29-JUN-08 C00009 A002
200100 1000 600 08-JAN-08 C00015 A003
200110 3000 500 15-APR-08 C00019 A010
200107 4500 900 30-AUG-08 C00007 A010
200112 2000 400 30-MAY-08 C00016 A007
200113 4000 600 10-JUN-08 C00022 A002
200102 2000 300 25-MAY-08 C00012 A012
Sample table: customer

+-----------+-------------+-------------+--------------+--------------
+-------+-------------+-------------+-------------+---------------
+--------------+------------+
|CUST_CODE | CUST_NAME | CUST_CITY | WORKING_AREA | CUST_COUNTRY |
GRADE | OPENING_AMT | RECEIVE_AMT | PAYMENT_AMT |OUTSTANDING_AMT| PHONE_NO
| AGENT_CODE |
+-----------+-------------+-------------+--------------+--------------
+-------+-------------+-------------+-------------+---------------
+--------------+------------+
| C00013 | Holmes | London | London | UK |
2 | 6000.00 | 5000.00 | 7000.00 | 4000.00 | BBBBBBB
| A003 |
| C00001 | Micheal | New York | New York | USA |
2 | 3000.00 | 5000.00 | 2000.00 | 6000.00 | CCCCCCC
| A008 |
| C00020 | Albert | New York | New York | USA |
3 | 5000.00 | 7000.00 | 6000.00 | 6000.00 | BBBBSBB
| A008 |
| C00025 | Ravindran | Bangalore | Bangalore | India |
2 | 5000.00 | 7000.00 | 4000.00 | 8000.00 | AVAVAVA
| A011 |
| C00024 | Cook | London | London | UK |
2 | 4000.00 | 9000.00 | 7000.00 | 6000.00 | FSDDSDF
| A006 |
| C00015 | Stuart | London | London | UK |
1 | 6000.00 | 8000.00 | 3000.00 | 11000.00 | GFSGERS
| A003 |
| C00002 | Bolt | New York | New York | USA |
3 | 5000.00 | 7000.00 | 9000.00 | 3000.00 | DDNRDRH
| A008 |
| C00018 | Fleming | Brisban | Brisban | Australia |
2 | 7000.00 | 7000.00 | 9000.00 | 5000.00 | NHBGVFC
| A005 |
| C00021 | Jacks | Brisban | Brisban | Australia |
1 | 7000.00 | 7000.00 | 7000.00 | 7000.00 | WERTGDF
| A005 |
| C00019 | Yearannaidu | Chennai | Chennai | India |
1 | 8000.00 | 7000.00 | 7000.00 | 8000.00 | ZZZZBFV
| A010 |
| C00005 | Sasikant | Mumbai | Mumbai | India |
1 | 7000.00 | 11000.00 | 7000.00 | 11000.00 | 147-25896312
| A002 |
| C00007 | Ramanathan | Chennai | Chennai | India |
1 | 7000.00 | 11000.00 | 9000.00 | 9000.00 | GHRDWSD
| A010 |
| C00022 | Avinash | Mumbai | Mumbai | India |
2 | 7000.00 | 11000.00 | 9000.00 | 9000.00 | 113-12345678
| A002 |
| C00004 | Winston | Brisban | Brisban | Australia |
1 | 5000.00 | 8000.00 | 7000.00 | 6000.00 | AAAAAAA
| A005 |
| C00023 | Karl | London | London | UK |
0 | 4000.00 | 6000.00 | 7000.00 | 3000.00 | AAAABAA
| A006 |
| C00006 | Shilton | Torento | Torento | Canada |
1 | 10000.00 | 7000.00 | 6000.00 | 11000.00 | DDDDDDD
| A004 |
| C00010 | Charles | Hampshair | Hampshair | UK |
3 | 6000.00 | 4000.00 | 5000.00 | 5000.00 | MMMMMMM
| A009 |
| C00017 | Srinivas | Bangalore | Bangalore | India |
2 | 8000.00 | 4000.00 | 3000.00 | 9000.00 | AAAAAAB
| A007 |
| C00012 | Steven | San Jose | San Jose | USA |
1 | 5000.00 | 7000.00 | 9000.00 | 3000.00 | KRFYGJK
| A012 |
| C00008 | Karolina | Torento | Torento | Canada |
1 | 7000.00 | 7000.00 | 9000.00 | 5000.00 | HJKORED
| A004 |
| C00003 | Martin | Torento | Torento | Canada |
2 | 8000.00 | 7000.00 | 7000.00 | 8000.00 | MJYURFD
| A004 |
| C00009 | Ramesh | Mumbai | Mumbai | India |
3 | 8000.00 | 7000.00 | 3000.00 | 12000.00 | Phone No
| A002 |
| C00014 | Rangarappa | Bangalore | Bangalore | India |
2 | 8000.00 | 11000.00 | 7000.00 | 12000.00 | AAAATGF
| A001 |
| C00016 | Venkatpati | Bangalore | Bangalore | India |
2 | 8000.00 | 11000.00 | 7000.00 | 12000.00 | JRTVFDD
| A007 |
| C00011 | Sundariya | Chennai | Chennai | India |
3 | 7000.00 | 11000.00 | 7000.00 | 11000.00 | PPHGRTS
| A010 |
+-----------+-------------+-------------+--------------+--------------
+-------+-------------+-------------+-------------+---------------
+--------------+------------+

To get order number and order amount columns from orders


table aliased as 'a' and customer name and working area
columns from customer table aliased as 'b' after joining said
two tables with the following condition -

1. order amount of orders table matches any of the opening


amounts of customer table,

the following SQL statement can be used:

SQL Code:
SELECT a.ord_num,a.ord_amount,b.cust_name,b.working_area

FROM orders a,customer b

WHERE a.ord_amount

BETWEEN b.opening_amt AND b.opening_amt;


Output:
ORD_NUM ORD_AMOUNT CUST_NAME
WORKING_AREA
--------- ---------- ----------------------------------------
-------------
200110 3000 Micheal
New York
200101 3000 Micheal
New York
200108 4000 Cook
London
200119 4000 Cook
London
200113 4000 Cook
London
200108 4000 Karl
London
200119 4000 Karl
London
200113 4000 Karl
London

What is Inner Join in SQL?


The INNER JOIN selects all rows from both participating tables
as long as there is a match between the columns. An SQL
INNER JOIN is same as JOIN clause, combining rows from two
or more tables.

Syntax:
SELECT *
FROM table1 INNER JOIN table2
ON table1.column_name = table2.column_name;
OR
SELECT *
FROM table1
JOIN table2
ON table1.column_name = table2.column_name;
Pictorial Presentation:

The INNER JOIN in SQL joins two tables according to the


matching of a certain criteria using a comparison operator.

Example:

Here is an example of inner join in SQL between two tables.

Sample table: foods


+---------+--------------+-----------+------------+
| ITEM_ID | ITEM_NAME | ITEM_UNIT | COMPANY_ID |
+---------+--------------+-----------+------------+
| 1 | Chex Mix | Pcs | 16 |
| 6 | Cheez-It | Pcs | 15 |
| 2 | BN Biscuit | Pcs | 15 |
| 3 | Mighty Munch | Pcs | 17 |
| 4 | Pot Rice | Pcs | 15 |
| 5 | Jaffa Cakes | Pcs | 18 |
| 7 | Salt n Shake | Pcs | |
+---------+--------------+-----------+------------+

Sample table: company

+------------+---------------+--------------+
| COMPANY_ID | COMPANY_NAME | COMPANY_CITY |
+------------+---------------+--------------+
| 18 | Order All | Boston |
| 15 | Jack Hill Ltd | London |
| 16 | Akas Foods | Delhi |
| 17 | Foodies. | London |
| 19 | sip-n-Bite. | New York |
+------------+---------------+--------------+
To join item name, item unit columns from foods table and
company name, company city columns from company table,
with the following condition -

1. company_id of foods and company table must be same,

the following SQL statement can be used :

SQL Code:
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;

Output:
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

Example of SQL INNER JOIN using JOIN keyword

To get item name, item unit columns from foods table and
company name, company city columns from company table,
after joining these mentioned tables, with the following
condition -
1. company id of foods and company id of company table must
be same,

the following SQL statement can be used:

SQL Code:
SELECT foods.item_name,foods.item_unit,

company.company_name,company.company_city

FROM foods

JOIN company

ON foods.company_id =company.company_id;

Output:
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

Pictorial Presentation:
SQL INNER JOIN for all columns
To get all the columns from foods and company table after
joining, with the following condition -

1. company id of foods and company id of company table must


be same,

the following SQL statement can be used:

SQL Code:
SELECT *

FROM foods

JOIN company

ON foods.company_id =company.company_id;

Output:
ITEM_ID ITEM_NAME ITEM_ COMPAN COMPAN
COMPANY_NAME COMPANY_CITY
-------- ------------------------- ----- ------ ------
------------------------- -------------
1 Chex Mix Pcs 16 16 Akas
Foods Delhi
6 Cheez-It Pcs 15 15 Jack
Hill Ltd London
2 BN Biscuit Pcs 15 15 Jack
Hill Ltd London
3 Mighty Munch Pcs 17 17
Foodies. London
4 Pot Rice Pcs 15 15 Jack
Hill Ltd London
5 Jaffa Cakes Pcs 18 18 Order
All Boston

Difference between JOIN and INNER JOIN


JOIN returns all rows from tables where the key record of one
table is equal to the key records of another table.

The INNER JOIN selects all rows from both participating tables
as long as there is a match between the columns. An SQL
INNER JOIN is same as JOIN clause, combining rows from two
or more tables.

An inner join of A and B gives the result of A intersect B, i.e.


the inner part of a Venn diagram intersection.

Inner joins use a comparison operator to match rows from two


tables based on the values in common columns from each
table. For example, retrieving all rows where the student
identification number is the same for both the students and
courses tables.

Using JOIN Clause


SELECT * FROM

Table1 JOIN Table2

ON Table1.column_name=Table2.column_name;

Using INNER JOIN Clause


SELECT *

FROM Table1 INNER JOIN Table2

ON Table1.column_name= Table2.column_name;
Difference between INNER JOIN and OUTER JOIN
An INNER JOIN is such type of join that returns all rows from
both the participating tables where the key record of one table
is equal to the key records of another table. This type of join
required a comparison operator to match rows from the
participating tables based on a common field or column of both
the tables.

Where as the OUTER JOIN returns all rows from the


participating tables which satisfy the condition and also those
rows which do not match the condition will appear in this
operation. This result set can appear in three types of format -

The first one is LEFT OUTER JOIN, in this join includes all the
rows from a left table of JOIN clause and the unmatched rows
from a right table with NULL values for selected columns.

The second one is RIGHT OUTER JOIN, in this join includes all
rows from the right of JOIN cause and the unmatched rows
from the left table with NULL values for selected columns.

The last one in FULL OUTER JOIN, in this join, includes the
matching rows from the left and right tables of JOIN clause
and the unmatched rows from left and right table with NULL
values for selected columns.

Example:

Here is two table tableX and tableY and they have no


duplicate rows in each. In tableX the values ( A,B) are unique
and in tableY the values (E,F) are unique, but the values (C
and D) are common in both the tables.
Here is INNER JOIN
SELECT *

FROM tableX

INNER JOIN tableY on tableX.X = tableY.Y;

or
SELECT tableX.*,tableY.*

FROM tableX,tableY

WHERE tableX.X = tableY.Y;

Output:

Here only the matching of both tableX and tableY have


appeared in the result set.

Here is LEFT OUTER JOIN


SELECT tableX.*,tableY.*

FROM tableX,tableY

WHERE tableX.X = tableY.Y(+)

or
SELECT *

FROM tableX

LEFT OUTER JOIN tableY ON tableX.X= tableY.Y

Output:

Here all the rows from tableX that is left side of JOIN clause
and all the rows with NULL values for unmatched columns
from tableY that is the right side of JOIN clause have
appeared.

Here is RIGHT OUTER JOIN


SELECT * FROM tableX

RIGHT OUTER JOIN tableY ON tableX.X= tableY.Y

Output:

Here all the rows from tableY that is the right side of JOIN
clause and all the rows with NULL values for unmatched
columns from tableX that is left side of JOIN clause have
appeared.

Here is FULL OUTER JOIN


SELECT *

FROM tableX
FULL OUTER JOIN tableY ON tableX.X= tableY.Y

Output:

Here all the matching rows from tableX and tableY and all
the unmatched rows with NULL values for both the tables have
appeared.

INNER JOIN ON vs WHERE clause


The WHERE clause, what is done is that all records that match
the WHERE condition are included in the result set but an
INNER JOIN is that, data not matching the JOIN condition is
excluded from the result set.

Linking between two or more tables should be done using an


INNER JOIN ON clause but filtering on individual data
elements should be done with WHERE clause.

INNER JOIN is ANSI syntax whereas the WHERE syntax is


more relational model oriented.

The INNER JOIN is generally considered more readable and it


is a cartesian product of the tables, especially when you join
lots of tables but the result of two tables JOIN'ed can be
filtered on matching columns using the WHERE clause.

What is Natural Join in SQL?


We have already learned that an EQUI JOIN performs a JOIN
against equality or matching column(s) values of the
associated tables and an equal sign (=) is used as comparison
operator in the where clause to refer equality.
The SQL NATURAL JOIN is a type of EQUI JOIN and is
structured in such a way that, columns with the same name of
associated tables will appear once only.

Natural Join: Guidelines

- The associated tables have one or more pairs of identically


named columns.
- The columns must be the same data type.
- Don’t use ON clause in a natural join.

Syntax:
SELECT *
FROM table1
NATURAL JOIN table2;
Example:

Here is an example of SQL natural join between tow tables:

Sample table: foods


+---------+--------------+-----------+------------+
| ITEM_ID | ITEM_NAME | ITEM_UNIT | COMPANY_ID |
+---------+--------------+-----------+------------+
| 1 | Chex Mix | Pcs | 16 |
| 6 | Cheez-It | Pcs | 15 |
| 2 | BN Biscuit | Pcs | 15 |
| 3 | Mighty Munch | Pcs | 17 |
| 4 | Pot Rice | Pcs | 15 |
| 5 | Jaffa Cakes | Pcs | 18 |
| 7 | Salt n Shake | Pcs | |
+---------+--------------+-----------+------------+

Sample table: company

+------------+---------------+--------------+
| COMPANY_ID | COMPANY_NAME | COMPANY_CITY |
+------------+---------------+--------------+
| 18 | Order All | Boston |
| 15 | Jack Hill Ltd | London |
| 16 | Akas Foods | Delhi |
| 17 | Foodies. | London |
| 19 | sip-n-Bite. | New York |
+------------+---------------+--------------+
To get all the unique columns from foods and company tables,
the following SQL statement can be used:

SQL Code:
SELECT *

FROM foods

NATURAL JOIN company;

Output:
COMPANY_ID ITEM_ID ITEM_NAME ITEM_UNIT
COMPANY_NAME COMPANY_CITY
---------- ---------- ------------------------- ----------
------------------------- --------------
16 1 Chex Mix Pcs
Akas Foods Delhi
15 6 Cheez-It Pcs
Jack Hill Ltd London
15 2 BN Biscuit Pcs
Jack Hill Ltd London
17 3 Mighty Munch Pcs
Foodies. London
15 4 Pot Rice Pcs
Jack Hill Ltd London
18 5 Jaffa Cakes Pcs
Order All Boston

Pictorial presentation of the above Natural Join:


Difference between natural join and inner join
There is one significant difference between INNER JOIN and
NATURAL JOIN is the number of columns returned. See the
following example on company table and foods table :

SQL Code:
SELECT *

FROM company;

Output:
COMPANY_ID COMPANY_NAME COMPANY_CITY
---------- ------------------------- ---------------
18 Order All Boston
15 Jack Hill Ltd London
16 Akas Foods Delhi
17 Foodies. London
19 sip-n-Bite. New York
SQL Code:
SELECT *

FROM foods;

Output:
ITEM_ID ITEM_NAME ITEM_UNIT COMPANY_ID
---------- ------------------------- ---------- ----------
1 Chex Mix Pcs 16
6 Cheez-It Pcs 15
2 BN Biscuit Pcs 15
3 Mighty Munch Pcs 17
4 Pot Rice Pcs 15
5 Jaffa Cakes Pcs 18
7 Salt n Shake Pcs
The INNER JOIN of company and foods on company_id will
return :

SQL Code:
SELECT *

FROM company

INNER JOIN foods

ON company.company_id = foods.company_id;

Output:
COMPANY_ID COMPANY_NAME COMPANY_CITY ITEM_ID
ITEM_NAME ITEM_UNIT COMPANY_ID
---------- --------------- --------------- ----------
--------------- ---------- ----------
16 Akas Foods Delhi 1 Chex Mix
Pcs 16
15 Jack Hill Ltd London 6 Cheez-It
Pcs 15
15 Jack Hill Ltd London 2 BN
Biscuit Pcs 15
17 Foodies. London 3 Mighty
Munch Pcs 17
15 Jack Hill Ltd London 4 Pot Rice
Pcs 15
18 Order All Boston 5 Jaffa
Cakes Pcs 18
SQL Code:
SELECT *

FROM company

NATURAL JOIN foods;

Output:
COMPANY_ID COMPANY_NAME COMPANY_CITY ITEM_ID
ITEM_NAME ITEM_UNIT
---------- --------------- --------------- ----------
--------------- ----------
16 Akas Foods Delhi 1 Chex Mix
Pcs
15 Jack Hill Ltd London 6 Cheez-It
Pcs
15 Jack Hill Ltd London 2 BN
Biscuit Pcs
17 Foodies. London 3 Mighty
Munch Pcs
15 Jack Hill Ltd London 4 Pot Rice
Pcs
18 Order All Boston 5 Jaffa
Cakes Pcs

You might also like