Skip to content
BENIBIX Learn

SQL Join

Development🗃️ DatabasesRelational Databases

Also known as JOIN, Table Join

The part of a query that pairs rows from two tables. You get one result.

Think of it like

Think of it like matching socks from two piles. You pair each sock with the one that fits it.

Example

Emma wants each order with the buyer's name. Orders hold only a customer id, so she joins them to customers.

Why it matters

Data is split across tables to keep it clean. Joins put it back together when you need it.

Definition

A join is the part of an SQL query that puts rows from two tables side by side. It matches them on a shared value, most often a foreign key and the primary key it points to. The result looks like one table, built just for that question. Inner joins keep only matches, and left joins keep every row from the first table. Joins are how a relational database answers questions that span tables.

Example code

sql
SELECT customers.name, orders.total
FROM customers
JOIN orders ON orders.customer_id = customers.id;
-- Emma | 25.0
-- Emma | 40.0
-- Jack | 15.5
Each order comes back with the name of the person who placed it.

Common mistake

Leaving out the ON part. Then each row is paired with every other row, and the result is huge.

Key takeaways

  1. 1
    Two tables, one answer. Rows are matched side by side.
  2. 2
    Match on keys. Most often a foreign key and a primary key.
  3. 3
    Kinds of join. Inner keeps matches, left keeps all.