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;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.
| id | name |
|---|---|
| 1 | Maya |
| 2 | Leo |
| 3 | Nia |
| 4 | Omar |
| id | customer_id | total |
|---|---|---|
| 101 | 1 | 49 |
| 102 | 1 | 75 |
| 103 | 3 | 20 |
| 104 | 99 | 15 |
| 105 | NULL | 30 |
Match Customers.id with Orders.customer_id. This example deliberately includes customers without orders, repeated customer IDs, an unmatched order and a NULL key.
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…
| customer_id | name | order_id | order_customer_id | total |
|---|---|---|---|---|
| 1 | Maya | 101 | 1 | 49 |
| 1 | Maya | 102 | 1 | 75 |
| 3 | Nia | 103 | 3 | 20 |
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.
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.
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.
| Join | Rows included | Rows in this example |
|---|---|---|
INNER JOIN | Every matching customer–order pair. | 3 |
LEFT JOIN | Matching pairs and customers without an order. | 5 |
RIGHT JOIN | Matching pairs and orders without a customer. | 5 |
FULL OUTER JOIN | Matching pairs and unmatched rows from both tables. | 7 |
CROSS JOIN | Every 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.
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;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;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;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.
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.
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 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.
SELECT c.name, o.total
FROM Customers AS c
LEFT JOIN Orders AS o
ON c.id = o.customer_id
AND o.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;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.
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.
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.
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.
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.