Skip to content
Laviyx Learn

Try

SQL course menu

SQL JOINs Explained: Combine Data From Multiple Tables

An introduction to SQL JOINs for beginners: why data is split across tables, how ON matches rows, and the difference between INNER, LEFT, RIGHT, FULL OUTER, SELF and CROSS JOIN, with diagrams and examples.

Intermediate6 min readUpdated
On this page

Databases split information into separate tables: customers in one, orders in another. That keeps each fact in one place, but most questions need data from several tables at once. Which customer placed this order? Which products have never sold? A JOIN combines rows from two tables into one result, based on a matching column.

Why data lives in separate tables#

Imagine storing everything in one big table:

order_idcustomercityamount
101AshaMumbai250
102AshaMumbai120

If Asha moves to Pune, every one of her orders must be updated, and missing one leaves the data contradicting itself. So instead, customer details are stored once in customers, and each order stores only the customer's id. JOINs reunite the pieces at query time. (See SQL Database Basics for primary and foreign keys.)

The example tables#

customers

customer_idnamecity
1AshaMumbai
2RaviDelhi
3MeeraPune
4JohnChennai

orders

order_idcustomer_idamount
1011250
1021120
1032560
104590

Two details make these tables a good test of every JOIN type: Meera and John have no orders, and order 104 belongs to customer 5, who does not exist.

How a JOIN works#

SQLRun
SELECT c.name, o.order_id, o.amount
FROM customers c
JOIN orders o
  ON o.customer_id = c.customer_id;
  • FROM customers c JOIN orders o names the two tables (with short aliases).
  • ON o.customer_id = c.customer_id is the join condition: it says which rows belong together.

For each pair of rows where the condition is true, the database produces one combined row. The JOIN type decides what happens to rows that have no partner.

The JOIN types at a glance#

INNER JOIN of customers and orders: Only rows that match in both tables.customersorders
INNER JOIN
LEFT JOIN of customers and orders: Every row from the left table, plus matches from the right.customersorders
LEFT JOIN
RIGHT JOIN of customers and orders: Every row from the right table, plus matches from the left.customersorders
RIGHT JOIN
FULL OUTER JOIN of customers and orders: Every row from both tables, matched where possible.customersorders
FULL OUTER JOIN
TypeKeepsRows with our data
INNER JOINonly matching rows3: Asha ×2, Ravi
LEFT JOINall customers, matching orders5: adds Meera and John with NULL orders
RIGHT JOINall orders, matching customers4: adds order 104 with a NULL customer
FULL OUTER JOINeverything from both6: all of the above
CROSS JOINevery combination, no condition16: 4 customers × 4 orders
SELF JOINa table joined to itselfe.g. employees with their managers

INNER JOIN: only matches#

Output
name | order_id | amount
-----+----------+-------
Asha | 101      | 250
Asha | 102      | 120
Ravi | 103      | 560

LEFT JOIN: all customers#

Output
name  | order_id | amount
------+----------+-------
Asha  | 101      | 250
Asha  | 102      | 120
Ravi  | 103      | 560
Meera | NULL     | NULL
John  | NULL     | NULL

FULL OUTER JOIN: everything#

Output
name  | order_id | amount
------+----------+-------
Asha  | 101      | 250
Asha  | 102      | 120
Ravi  | 103      | 560
Meera | NULL     | NULL
John  | NULL     | NULL
NULL  | 104      | 90

NULL fills the columns of the side that had no match.

Choosing the right JOIN#

Ask one question: which rows must not disappear?

  • Only complete pairs matter → INNER JOIN.
  • Every row of the first table must appear → LEFT JOIN.
  • Every row of both tables must appear → FULL OUTER JOIN.
  • You need every combination (rare) → CROSS JOIN.

In practice, INNER JOIN and LEFT JOIN cover the vast majority of real queries. RIGHT JOIN is almost always rewritten as a LEFT JOIN with the tables swapped.

JOIN syntax rules#

  • JOIN on its own means INNER JOIN; LEFT JOIN means LEFT OUTER JOIN.
  • Prefix columns with their table alias (c.name, o.amount), especially columns that exist in both tables.
  • The ON condition usually connects a foreign key to the primary key it refers to.
  • You can join more than two tables by adding more JOIN ... ON ... clauses (see Multiple JOINs).

Common mistakes#

Practice#

Practice exercise

Without running anything, answer for the tables above:

  1. How many rows does customers INNER JOIN orders return?
  2. How many rows does customers LEFT JOIN orders return?
  3. Which join would you use to list every order, including order 104, with the customer's name where it exists?
Show hint

Count the matching pairs first, then add the unmatched rows that each join type keeps.

Show solution
  1. 3 rows: Asha's two orders and Ravi's one.
  2. 5 rows: the 3 matches plus Meera and John with NULL order columns.
  3. orders LEFT JOIN customers (or, equivalently, customers RIGHT JOIN orders). Order 104 appears with a NULL name.

Key takeaway#

Start with the most common type: INNER JOIN.

Finished this lesson?