Databases & SQL

SQL JOIN

Understand SQL joins by running them. Compare INNER, LEFT, RIGHT, FULL and CROSS JOIN with sample tables, editable SQL and live results in your browser.

Join two tables. See every row.SQLite in your browser. No account required.
Customers
idname
1Maya
2Leo
3Nia
4Omar
Orders
idcustomer_idtotal
101149
102175
103320
1049915
105NULL30

Match Customers.id with Orders.customer_id. This example deliberately includes customers without orders, repeated customer IDs, an unmatched order and a NULL key.

Use the sample

Keep only matching customer and order pairs. Maya appears twice because she has two orders.

Edit the query and choose Run SQL, or press Ctrl+Enter / ⌘+Enter. Choosing a join example replaces the query. Each run starts with the sample tables above; changes are not saved.

Loading the SQL engine…

INNER JOIN example: 3 rows
customer_idnameorder_idorder_customer_idtotal
1Maya101149
1Maya102175
3Nia103320

The editor runs real SQLite queries locally. It shows up to 200 rows per result and five result sets; long cell values are shortened after 4,000 characters. Long-running queries are stopped. SQL dialects differ, so check syntax when moving a query to another database.

What is a SQL join?

A SQL join combines rows from two table references. Most joins use a condition to decide which rows belong together, such as a customer’s ID matching an order’s customer ID. The join type decides what happens when a row has no match.

In the example, Maya has two orders, so an INNER JOIN returns two rows for Maya. Leo and Omar have no orders. Order 104 points to a customer ID that is absent, and order 105 has no customer ID.

Basic join syntax
SELECT a.column_name, b.column_name
FROM first_table AS a
INNER JOIN second_table AS b
  ON a.key_column = b.key_column;

Aliases such as a and b shorten names and make it clear which table supplies each column.

SQL join types compared

JoinRows includedRows in this example
INNER JOINEvery matching customer–order pair.3
LEFT JOINMatching pairs and customers without an order.5
RIGHT JOINMatching pairs and orders without a customer.5
FULL OUTER JOINMatching pairs and unmatched rows from both tables.7
CROSS JOINEvery possible pair, with no matching condition.20

“OUTER” is optional in LEFT OUTER JOIN, RIGHT OUTER JOIN and FULL OUTER JOIN. Plain JOIN normally means INNER JOIN.

SQL join examples

Find customers with orders

Use INNER JOIN when only matching pairs should appear.

SELECT c.name, o.id AS order_id
FROM Customers AS c
INNER JOIN Orders AS o
  ON c.id = o.customer_id;

Find customers without orders

Keep all customers with LEFT JOIN, then select the rows with no matching order.

SELECT c.id, c.name
FROM Customers AS c
LEFT JOIN Orders AS o
  ON c.id = o.customer_id
WHERE o.id IS NULL;

Total orders per customer

LEFT JOIN includes customers with no orders. Count the matched order IDs, then group the rows.

SELECT c.name, COUNT(o.id) AS orders
FROM Customers AS c
LEFT JOIN Orders AS o
  ON c.id = o.customer_id
GROUP BY c.id, c.name;

Join a table to itself

A self-join uses two aliases for the same table. For an Employees table with id and manager_id columns, this pairs employees with their managers.

SELECT e.name, m.name AS manager
FROM Employees AS e
LEFT JOIN Employees AS m
  ON e.manager_id = m.id;

Employees is a separate example schema, not one of the playground’s initial tables.

Three details that change your results

Matching keys can produce more rows

A join returns each matching pair, not one row per key. If one table has two rows for a key and the other has three, that key produces six joined rows. Check your relationship and join condition before using DISTINCT to remove rows.

NULL is not equal to NULL

A condition such as a.id = b.id does not match two NULL values. Ordinary equality with NULL does not evaluate to true. Use IS NULL to find missing values; use an appropriate null-safe comparison only when matching missing keys is intentional.

ON and WHERE can have different effects

ON decides which rows match. WHERE filters the joined result. With an outer join, moving a condition between them can remove the unmatched rows you wanted to keep.

Keep every customer
SELECT c.name, o.total
FROM Customers AS c
LEFT JOIN Orders AS o
  ON c.id = o.customer_id
 AND o.total >= 50;
Keep only results with total ≥ 50
SELECT c.name, o.total
FROM Customers AS c
LEFT JOIN Orders AS o
  ON c.id = o.customer_id
WHERE o.total >= 50;

Check your understanding

Which join keeps every customer, even without an order?

Assume the query starts with FROM Customers and joins Orders.

Common questions

What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN returns only matching pairs. LEFT JOIN also keeps rows from the left table that found no match, filling right-side columns with NULL.

Can I join more than two tables?

Yes. Add another JOIN with its own condition. For example, you could join Customers to Orders, then Orders to OrderItems using the order ID. Use clear aliases and check each relationship so you do not accidentally multiply rows.

Does FULL OUTER JOIN work in MySQL?

MySQL does not directly support FULL OUTER JOIN syntax. One common alternative combines a LEFT JOIN with only the unmatched rows from the other side using UNION ALL. The playground uses SQLite, which supports FULL OUTER JOIN.

Why are there duplicate-looking rows?

Several rows may share the same join key. The result includes every matching pair, so columns you did not select can explain why two visible rows look identical. Check the keys and include identifying columns while debugging.

Are my SQL queries uploaded or saved?

No. Queries execute locally in your browser against an in-memory sample database. Each run starts fresh. Nothing you enter is uploaded by this tool, and reloading clears the editor. CSV downloads are saved only when you request them.