# SQL JOIN

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

- Level: Intermediate
- Topics: Data
- URL: https://tidbit.legios.com.ar/sql-join/
- In Spanish: [JOIN en SQL](https://tidbit.legios.com.ar/es/sql-join/)

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.

## An analogy

Like matching a guest list with a seating chart by name: an inner join lists only guests who have a seat, a left join lists every guest and marks who is still waiting for a chair.

## Keep in mind

- Combines rows from two tables using a shared column, set in the ON clause.
- INNER JOIN keeps only matches; LEFT JOIN keeps every row from the first table.
- Joining on a column with repeated values can multiply rows and inflate totals.
- Most data models rely on primary and foreign keys to make joins reliable.

## Go deeper

- [Joins between tables (PostgreSQL tutorial)](https://www.postgresql.org/docs/current/tutorial-join.html)
- [Join (SQL) on Wikipedia](https://en.wikipedia.org/wiki/Join_(SQL))

## Related terms

- [SQL](https://tidbit.legios.com.ar/sql/): The language you use to ask a database questions.
- [Database](https://tidbit.legios.com.ar/database/): An organized store of data that programs can search and update safely.
- [Primary and foreign keys](https://tidbit.legios.com.ar/primary-and-foreign-keys/): A primary key names each row uniquely; a foreign key points to a row in another table.
