Estimated reading time: 5 minutes

SQL Joins Explained with Examples

SQL Joins Explained with Examples

SQL joins are used to combine rows from two or more tables based on a related column between them. They allow you to retrieve data from multiple tables in a single result set. Here’s a breakdown of the common types of SQL joins with illustrative examples.

Sample Data

Let’s assume we have two tables: Customers and Orders.

CustomerIDCustomerNameCity
1Alfreds FutterkisteBerlin
2Berglunds snabbköpLuleå
3Centro comercial MoctezumaMéxico D.F.
4Around the HornLondon
5Exotic LiquidsLondon
6New CustomerParis

Customers Table

OrderIDCustomerIDOrderDate
1024812025-04-01
1024922025-04-02
1025032025-04-03
1025112025-04-04
1025242025-04-05
10253NULL2025-04-06

Orders Table

1. INNER JOIN (or JOIN)

The INNER JOIN returns rows only when there is a match in both tables based on the specified join condition.

SELECT Customers.CustomerName, Orders.OrderID, Orders.OrderDate
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Retrieving customers and their corresponding orders.

Result:

CustomerNameOrderIDOrderDate
Alfreds Futterkiste102482025-04-01
Berglunds snabbköp102492025-04-02
Centro comercial Moctezuma102502025-04-03
Alfreds Futterkiste102512025-04-04
Around the Horn102522025-04-05

Notice that “New Customer” (CustomerID 6) is not included because there are no matching orders, and the order with OrderID 10253 is not included because its CustomerID is NULL and doesn’t match any CustomerID in the Customers table.

2. LEFT JOIN (or LEFT OUTER JOIN)

The LEFT JOIN returns all rows from the left table (Customers in this case), and the matching rows from the right table (Orders). If there is no match in the right table, NULL values are used for the columns from the right table.

SELECT Customers.CustomerName, Orders.OrderID, Orders.OrderDate
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Retrieving all customers and their orders (if any).

Result:

CustomerNameOrderIDOrderDate
Alfreds Futterkiste102482025-04-01
Berglunds snabbköp102492025-04-02
Centro comercial Moctezuma102502025-04-03
Around the Horn102522025-04-05
Exotic LiquidsNULLNULL
New CustomerNULLNULL
Alfreds Futterkiste102512025-04-04

All customers are listed. Customers with no orders have NULL values for OrderID and OrderDate.

3. RIGHT JOIN (or RIGHT OUTER JOIN)

The RIGHT JOIN returns all rows from the right table (Orders), and the matching rows from the left table (Customers). If there is no match in the left table, NULL values are used for the columns from the left table.

SELECT Customers.CustomerName, Orders.OrderID, Orders.OrderDate
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Retrieving all orders and their corresponding customers (if any).

Result:

CustomerNameOrderIDOrderDate
Alfreds Futterkiste102482025-04-01
Berglunds snabbköp102492025-04-02
Centro comercial Moctezuma102502025-04-03
Around the Horn102522025-04-05
NULL102532025-04-06
Alfreds Futterkiste102512025-04-04

All orders are listed. The order with CustomerID as NULL has NULL for CustomerName.

4. FULL OUTER JOIN (or FULL JOIN)

The FULL OUTER JOIN returns all rows when there is a match in either the left table (Customers) or the right table (Orders). If there is no match in one of the tables, NULL values are used for the columns from the table without a match. (Note: Some older SQL databases may not fully support FULL OUTER JOIN).

SELECT Customers.CustomerName, Orders.OrderID, Orders.OrderDate
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Retrieving all customers and all orders, with NULLs where there’s no match.

Result:

CustomerNameOrderIDOrderDate
Alfreds Futterkiste102482025-04-01
Berglunds snabbköp102492025-04-02
Centro comercial Moctezuma102502025-04-03
Around the Horn102522025-04-05
Exotic LiquidsNULLNULL
New CustomerNULLNULL
NULL102532025-04-06
Alfreds Futterkiste102512025-04-04

This result includes all customers and all orders. Where a customer has no orders, the order columns are NULL. Where an order has no matching customer, the customer columns are NULL.

5. CROSS JOIN

The CROSS JOIN returns the Cartesian product of the rows from the joined tables. This means it combines each row from the first table with each row from the second table. It typically doesn’t have an ON clause.

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
CROSS JOIN Orders;

Creating all possible combinations of customers and orders.

Result (Partial):

CustomerNameOrderID
Alfreds Futterkiste10248
Alfreds Futterkiste10249
Alfreds Futterkiste10250
Alfreds Futterkiste10251
Alfreds Futterkiste10252
Alfreds Futterkiste10253
Berglunds snabbköp10248
Berglunds snabbköp10249

This will produce a result set with 6 (customers) * 6 (orders) = 36 rows.

6. Self JOIN

A SELF JOIN is used to join a table to itself. This is useful when the table has a hierarchical relationship or when you need to compare rows within the same table.

Let’s imagine an Employees table with EmployeeID and ManagerID columns.

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    EmployeeName VARCHAR(255),
    ManagerID INT,
    FOREIGN KEY (ManagerID) REFERENCES Employees(EmployeeID)
);

INSERT INTO Employees (EmployeeID, EmployeeName, ManagerID) VALUES
(1, 'John Smith', NULL),
(2, 'Jane Doe', 1),
(3, 'Peter Jones', 1),
(4, 'Mary Brown', 2),
(5, 'David Wilson', 3);

SELECT e1.EmployeeName AS Employee, e2.EmployeeName AS Manager
FROM Employees e1
INNER JOIN Employees e2 ON e1.ManagerID = e2.EmployeeID;

Finding each employee and their manager.

Result:

EmployeeManager
Jane DoeJohn Smith
Peter JonesJohn Smith
Mary BrownJane Doe
David WilsonPeter Jones

Understanding these different types of SQL joins is fundamental for querying and combining data from multiple tables effectively. The choice of join depends on the specific data you need to retrieve and the relationships between the tables involved.

Agentic AI (45) AI (2) AI Agent (25) airflow (3) Algorithm (45) Algorithms (108) apache (32) apex (11) API (118) Automation (68) Autonomous (84) auto scaling (5) AWS (63) aws bedrock (1) Azure (56) Banks (1) BigQuery (23) bigtable (3) blockchain (9) Career (9) Chatbot (26) cloud (166) cpu (54) cuda (13) Cybersecurity (30) database (89) Databricks (20) Data structure (22) Design (109) dynamodb (12) ELK (3) embeddings (49) emr (3) Finance (4) flink (10) gcp (21) Generative AI (40) gpu (41) graph (57) graph database (15) graphql (3) Healthcare (2) image (87) indexing (40) interview (11) java (45) json (39) Kafka (20) LLM (51) LLMs (75) market analysis (2) Market report (1) market summary (2) Mcp (6) monitoring (130) Monolith (3) mulesoft (8) N8n (9) Networking (18) NLU (5) node.js (19) Nodejs (3) nosql (22) Optimization (104) performance (254) Platform (149) Platforms (124) postgres (5) productivity (39) programming (71) pseudo code (1) python (89) pytorch (33) Q&A (4) RAG (51) rasa (5) rdbms (6) ReactJS (1) realtime (2) redis (11) Restful (7) rust (3) S3 (1) salesforce (25) Spark (32) spring boot (4) sql (79) stock (14) stock analysis (1) stock market (2) tensor (15) time series (17) tips (11) tricks (20) undervalued stocks (2) use cases (144) vector (73) vector db (8) Vertex AI (23) Workflow (68)