What Is SQL Joins
What Is SQL Joins
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.
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.
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:
Syntax:
FROM table1
join_type table2
[ON (join_condition)]
Parameters:
Name Description
Example:
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
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:
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 -
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.
Syntax:
SELECT *
FROM table_name1, table_name2
WHERE table_name1.column [> | < | >= | <= ]
table_name2.column;
Pictorial representation:
Example:
+-----------+-------------+-------------+--------------+--------------
+-------+-------------+-------------+-------------+---------------
+--------------+------------+
|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 |
+-----------+-------------+-------------+--------------+--------------
+-------+-------------+-------------+-------------+---------------
+--------------+------------+
SQL Code:
SELECT a.ord_num,a.ord_amount,b.cust_name,b.working_area
WHERE a.ord_amount
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:
Example:
+------------+---------------+--------------+
| 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 -
SQL Code:
SELECT foods.item_name,foods.item_unit,
company.company_name,company.company_city
FROM foods
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
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,
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 -
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
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.
ON Table1.column_name=Table2.column_name;
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.
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:
FROM tableX
or
SELECT tableX.*,tableY.*
FROM tableX,tableY
Output:
FROM tableX,tableY
or
SELECT *
FROM tableX
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.
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.
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.
Syntax:
SELECT *
FROM table1
NATURAL JOIN table2;
Example:
+------------+---------------+--------------+
| 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
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
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
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
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