Learn SQL Lesson

Why JOINs Exist

When a database is normalized, related facts live in different tables. That keeps the data clean, but it also means you need a way to bring those facts back together when you query.

That is what `JOIN` does. It combines rows from two tables based on a matching key.

For example, `employees.department_id` points to `departments.id`. The `employees` table knows which department an employee belongs to, and the `departments` table knows the department name. A `JOIN` lets you combine both facts into one result.

The most important thing in a `JOIN` is the matching condition:

ON employees.department_id = departments.id

If you understand which columns connect the tables, the rest of the `JOIN` becomes much easier to reason about.

Open interactive editor