Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

sql
joins

What is a natural join in SQL?

Educative Answers Team

​The join clause is used to combine tables based on a common column and a join condition. A natural join is a type of join that combines tables based on columns with the same name and type.

Remember:

  • There is no need to specify the column names to join.
  • The resultant table does not contain repeated columns.
  • It is possible to perform a natural join on more than two tables.
svg viewer

Syntax

SELECT * FROM
TableName1 NATURAL JOIN TableName2;

After the SELECT keyword, the names of the columns that are to be included in the result of the query are specified. The * operator is used if all the columns need to be selected.

After the FROM keyword, the tables which need to be joined are specified, ​and the NATURAl JOIN keyword is written between the table names.


Example

/*Creating the tables*/
CREATE TABLE product_list(
  ID varchar(20),
  Pname varchar(20)
);
CREATE TABLE product_details(
  ID varchar(20),
  Brand varchar(20),
  Origin varchar(20)
);
CREATE TABLE brand_details(
  Brand varchar(20),
  OfficeAddress varchar(20)
);
/*Inserting values into product_list*/
INSERT INTO product_list(ID, Pname)
VALUES("1", "Apples");
INSERT INTO product_list(ID, Pname)
VALUES("2", "Oranges");
INSERT INTO product_list(ID, Pname)
VALUES("3", "Mangoes");
/*Inserting values into product_details*/
INSERT INTO product_details(ID, Brand, Origin)
VALUES("1", "Fresh Foods", "USA");
INSERT INTO product_details(ID, Brand, Origin)
VALUES("2", "Angro Ltd", "Pakistan");
/*Inserting values into brand_details*/
INSERT INTO brand_details(Brand, OfficeAddress)
VALUES("Fresh Foods", "123 Seattle USA");
INSERT INTO brand_details(Brand, OfficeAddress)
VALUES("Angro Ltd", "124 Lahore");
/*Performing the natural join with two tables*/
SELECT * FROM 
product_list NATURAL JOIN product_details;
/*Performing the natural join with three tables*/
SELECT * FROM 
product_list NATURAL JOIN product_details
NATURAL JOIN brand_details;

RELATED TAGS

sql
joins
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring