SQL Natural Join - W3resource
SQL Natural Join - W3resource
w3resource
SQL Natural Join
Last update on November 08 2016 05:07:38 (UTC/GMT +8 hours)
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
http://www.w3resource.com/sql/joins/naturaljoin.php 1/9
12/2/2016 SQL natural join w3resource
+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+
| 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 |
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 :
01. SELECT *
02. FROM foods
03. 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 CheezIt 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 :
http://www.w3resource.com/sql/joins/naturaljoin.php 2/9
12/2/2016 SQL natural join w3resource
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 :
01. SELECT *
02. FROM company;
Output
http://www.w3resource.com/sql/joins/naturaljoin.php 3/9
12/2/2016 SQL natural join w3resource
COMPANY_ID COMPANY_NAME COMPANY_CITY
18 Order All Boston
15 Jack Hill Ltd London
16 Akas Foods Delhi
17 Foodies. London
19 sipnBite. New York
01. SELECT *
02. FROM foods;
Output
ITEM_ID ITEM_NAME ITEM_UNIT COMPANY_ID
1 Chex Mix Pcs 16
6 CheezIt 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 :
01. SELECT *
02. FROM company
03. INNER JOIN foods
04. 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 CheezIt 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
01. SELECT *
http://www.w3resource.com/sql/joins/naturaljoin.php 4/9
12/2/2016 SQL natural join w3resource
02. FROM company
03. 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 CheezIt 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
NATURAL JOINS: Relational Databases
Oracle NATURAL JOIN
MySQL NATURAL JOIN
SQLite NATURAL JOIN
Key points to remember
Click on the following to get the slides presentation
http://www.w3resource.com/sql/joins/naturaljoin.php 5/9
12/2/2016 SQL natural join w3resource
Is this content useful for you?
Yes No
http://www.w3resource.com/sql/joins/naturaljoin.php 6/9
12/2/2016 SQL natural join w3resource
6 Comments w3resource
1 Login
Join the discussion…
tayyor • a year ago
" on " clause and "using " clause can be used in a natural join for sqlplus
△ ▽ • Reply • Share ›
rahul gupta • a year ago
good explanations
△ ▽ • Reply • Share ›
Adil Memon • 2 years ago
thank you so much for the explanation.
△ ▽ • Reply • Share ›
thisisanam • 2 years ago
my prof still hasn't shown us how to open postgres or told us the password to the schools server
which is required to make our own for some reason. we have a midterm in 2 days....paying this
much money to teach myself is stupid. thanks for the clear outline of how this works.
△ ▽ • Reply • Share ›
Kevin • 4 years ago
Only good explaination I've seen thus far.
13 △ ▽ • Reply • Share ›
pravin • 4 years ago
thank u .........
12 △ ▽ • Reply • Share ›
©w3resource 201115
http://www.w3resource.com/sql/joins/naturaljoin.php 7/9
12/2/2016 SQL natural join w3resource
Privacy policy
About
Contact
Feedback
Advertise
http://www.w3resource.com/sql/joins/naturaljoin.php 8/9
12/2/2016 SQL natural join w3resource
http://www.w3resource.com/sql/joins/naturaljoin.php 9/9