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

SQL Interview Question at Zomato for a Data analyst Position (0-3 Years) – | Shared By An Experienced Data Analyst

SQL Interview Question at Zomato: These questions were recently asked in interview at Zomato, you…

6 days ago

The Ultimate Guide to SQL Indexing and Query Optimization

Introduction: SQL Indexing and Query Optimization SQL indexing is a critical concept that can drastically…

2 weeks ago

SQL Interview Questions Asked In Walmart For Data Analyst Post | CTC – 18 LPA | Learn With Curious Club!!

This article is about the SQL Interview Questions asked by Walmart for their Data Analyst…

2 weeks ago

SQL Interview Questions for Deloitte Data Engineer Roles: Your Ultimate Prep Guide

You must be able to answer these SQL Interview Questions if you are applying for…

3 weeks ago

Data Analyst SQL Interview Questions | EY (Ernst & Young) | Shared By An Experienced Data Analyst

This article tackles common SQL Interview Questions asked by EY, offering detailed solutions and explanations…

3 weeks ago

1164 Product Price at a Given Date

1164. Product Price at a Given Date: Learn how to track and select price from…

1 month ago