Introduction
When working with databases, data is often stored in multiple tables instead of one large table. This helps keep data organized, efficient, and scalable. But in real-world applications, we frequently need to combine data from different tables to get meaningful insights.
This is where SQL Joins come into play.
SQL Joins allow you to combine rows from two or more tables based on a related column between them. Understanding joins is one of the most important skills for beginners in SQL, especially if you want to work with real-world data such as user information, orders, products, or transactions.
In this article, you will learn how SQL joins work, the different types of joins, and how to use them with simple, real-world examples.
What is a Join in SQL?
A JOIN is used to combine data from multiple tables based on a common column.
For example:
A Users table stores user details
An Orders table stores order details
👉 To see which user placed which order, we use a JOIN.
Tables for Understanding
Let’s consider two tables:
Users Table
UserID | Name
1 | Rahul
2 | Priya
3 | Amit
Orders Table
OrderID | UserID | Product
101 | 1 | Laptop
102 | 2 | Phone
103 | 1 | Headphones
Types of SQL Joins
There are mainly four types of joins:
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
Let’s understand each one with examples.
1. INNER JOIN
INNER JOIN returns only the matching records from both tables.
SELECT Users.Name, Orders.Product
FROM Users
INNER JOIN Orders
ON Users.UserID = Orders.UserID;
Output Explanation
This query will return:
Rahul → Laptop
Rahul → Headphones
Priya → Phone
👉 Amit is not included because there is no matching order.
Real-World Use Case
Fetch customers who have placed orders
Show employees who are assigned to projects
2. LEFT JOIN
LEFT JOIN returns all records from the left table and matching records from the right table.
SELECT Users.Name, Orders.Product
FROM Users
LEFT JOIN Orders
ON Users.UserID = Orders.UserID;
Output Explanation
Rahul → Laptop
Rahul → Headphones
Priya → Phone
Amit → NULL
👉 Amit appears even though he has no orders.
Real-World Use Case
Find users who have NOT placed any orders
Show all customers, including inactive ones
3. RIGHT JOIN
RIGHT JOIN returns all records from the right table and matching records from the left table.
SELECT Users.Name, Orders.Product
FROM Users
RIGHT JOIN Orders
ON Users.UserID = Orders.UserID;
Output Explanation
Rahul → Laptop
Rahul → Headphones
Priya → Phone
👉 If an order exists without a user, it will still appear.
Real-World Use Case
Find all orders, even if user data is missing
4. FULL JOIN
FULL JOIN returns all records from both tables.
SELECT Users.Name, Orders.Product
FROM Users
FULL JOIN Orders
ON Users.UserID = Orders.UserID;
Output Explanation
Includes all users and all orders
Non-matching data will show NULL
Real-World Use Case
Data analysis and reporting
Finding missing relationships
Join the conversation! Your thoughts help the community grow.