Learn SQL Lesson

INNER JOIN Basics

An `INNER JOIN` keeps only rows that match on both sides.

For example, `employees.department_id` points to `departments.id`. To show each employee with their department name, you can write:

SELECT e.name AS employee_name, d.name AS department_name
FROM employees AS e
INNER JOIN departments AS d
  ON e.department_id = d.id

The `ON` clause defines how the rows relate. With `INNER JOIN`, employees that do not have a matching department are excluded.

Use `INNER JOIN` when you only want records that exist in both tables.

Practice challenge

List employee names with their department names for employees that have a matching department. Return employee_name and department_name, ordered by employee_name.

Open interactive editor