JOIN (SQL)

A JOIN combines rows from two or more tables based on a related column, usually a foreign key matching a primary key. It lets you pull together data that is spread across tables.

SQL

Because relational databases split related information into separate tables, joins are how you stitch it back together — for example, showing each order alongside its customer's name.

The main types are INNER JOIN (only matching rows), LEFT JOIN (all rows from the left table plus matches), RIGHT JOIN, and FULL JOIN. The ON clause defines how the tables relate.

Syntax

SELECT columns
FROM table_a
JOIN table_b ON table_a.key = table_b.key;

Examples

SQL

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

Output:

id  name
--  -----
1   Alice
2   Bob

Learn it in a lesson

See also

Back to the Dictionary