JOIN — Combine Data from Multiple Tables

In SQL, a JOIN is used to combine rows from two or more tables based on a related column between them.

Joins are helpful when your data is spread across multiple tables — like customers and their orders — and you want to see it together in a single result.


Example Scenario

Let's say we have two tables:

customers

+-------------+--------+-------------+-----------+
| customer_id | name   | city        | country   |
+-------------+--------+-------------+-----------+
| 1           | Alice  | New York    | USA       |
| 2           | Bob    | Los Angeles | USA       |
| 3           | Emma   | London      | UK        |
| 4           | Paul   | Berlin      | Germany   |
+-------------+--------+-------------+-----------+

orders

+----------+-------------+--------------+--------------+
| order_id | customer_id | product_name | order_status |
+----------+-------------+--------------+--------------+
| 101      | 1           | Laptop       | Delivered    |
| 102      | 1           | Phone        | Shipped      |
| 103      | 2           | Charger      | Cancelled    |
| 104      | 3           | Watch        | Delivered    |
| 105      | 4           | Headphones   | Shipped      |
| 106      | 4           | Speakers     | Pending      |
+----------+-------------+--------------+--------------+

And imagine we want to queries like

  1. We want to see customer name & the products they orderd in one view (product_name is in orders table, customer name is in customers table)
  2. We want to know the locations of pending orders (the order_status is in orders table and city, country are in customers table)

Note: Understand these tables, because we use these tables instead of the employees table in the


Basic JOIN Syntax

SELECT table1.column_name, table2.column_name
FROM table1
JOIN table2
ON table1.common_column = table2.common_column;

Example Query

SELECT customers.name, orders.product_name
FROM customers
JOIN orders
ON customers.customer_id = orders.customer_id;

This query joins the two tables where customers.customer_id matches orders.customer_id, and shows names from both.


In the next few topics, we'll go through different types of joins — like INNER JOIN, LEFT JOIN, and more — to understand how they work and when to use each.