What is SQL Natural Join?

Natual Join is a type of join in SQL which combines row from 2 tables based on common column which has same name and datatype. It automatically matches the 2 columns and eliminates the duplicate column. read more

Example

Database

CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    Email VARCHAR(100),
    City VARCHAR(50)
);
CustomerIDFirstNameLastNameEmailCity
1JohnDoejohn.doe@example.comNew York
2JaneSmithjane.smith@example.comLos Angeles
3EmilyJohnsonemily.j@example.comChicago

CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderDate DATE,
    TotalAmount DECIMAL(10, 2),
    City VARCHAR(50),
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
OrderIDCustomerIDOrderDateTotalAmountCity
10112024-01-10250.50New York
10222024-01-11150.00Los Angeles
10332024-01-12300.75Chicago

Syntax

SELECT *

FROM TABLE1

NATURAL JOIN TABLE2;

Query

SELECT * 
FROM Orders
NATURAL JOIN Customers;

SQL Natural Join

Explanation

In this scenario, SQL combines the Orders and Customers tables using a natural join. The join is performed based on the common columns, CustomerID and City, which appear at the beginning of the resulting table.

As a result, SQL brings together columns like FirstName, LastName, and Email from the Customers table, along with OrderID, OrderDate, and TotalAmount from the Orders table. The shared columns, CustomerID and City, are seamlessly included in the result without duplication, making the output clear and concise.


I hope this explanation was helpful for you, consider sharing this with your friends. thank you.

Spread the love

Recent Posts

Mastering Pivot Table in Python: A Comprehensive Guide

Pivot tables are a powerful tool for summarizing and analyzing data, and Python’s Pandas library…

1 week ago

Data Science Interview Questions Section 3: SQL, Data Warehousing, and General Analytics Concepts

Welcome to Section 3 of our Data Science Interview Questions series! In this part, we…

1 week ago

Data Science Interview Questions Section 2: 25 Questions Designed To Deepen Your Understanding

Welcome back to our Data Science Interview Questions series! In the first section, we explored…

2 weeks ago

Data Science Questions Section 1: Data Visualization & BI Tools (Power BI, Tableau, etc.)

Data Science Questions in Section 1 focus on the essential concepts of Data Visualization and…

2 weeks ago

Optum Interview Questions: 30 Multiple Choice Questions (MCQs) with Answers

In this article, we’ve compiled 30 carefully selected multiple choice questions (MCQs) with answers to…

2 weeks ago

Day 15 of Learning Python for Data Science: Exploring Matplotlib Visualizations and EDA

Welcome to Day 15 of our Python for Data Science journey!On Day 15, we dived…

2 weeks ago