ENES

SQL JOIN

The SQL command that combines rows from two tables that share a value.

A customers table and an orders table connected by matching customer IDs, combined into one result table with names next to order amounts

A JOIN is how SQL puts together data that lives in separate tables. A shop keeps customers in one table and orders in another, linked by a customer ID. To list each order next to the customer's name, you write SELECT … FROM orders JOIN customers ON orders.customer_id = customers.id, and the database matches every order with its customer.

There are a few kinds, and the difference is what happens to rows without a match. An INNER JOIN keeps only pairs that match: customers who have orders. A LEFT JOIN keeps every row from the first table and fills in blanks where the second has nothing, which is how you find customers who never bought anything. RIGHT and FULL OUTER JOIN are the mirror image and the both-sides version.

Joins are why relational databases split data into tables in the first place: store each fact once, then combine on demand. They are also a classic source of mistakes. Join on the wrong column, or on one that repeats, and rows multiply silently, so a sales total suddenly comes out double. Checking row counts before and after a join is a good habit.

Keep in mind

Go deeper

Related terms

More in Data