Chapter 6 · Lesson 2 of 7
INNER JOIN Basics
Combine two tables with INNER JOIN and an ON condition, keeping only rows that match on both sides, like employees and departments.
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.idThe 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.
Sample data
This lesson loads the employees + departments tables into DuckDB in your browser with one click, so you can run every example without uploading anything.
Setup SQL for employees, departments
CREATE OR REPLACE TABLE departments (
id INTEGER,
name VARCHAR
);
INSERT INTO departments VALUES
(1, 'Engineering'),
(2, 'Marketing'),
(3, 'Sales'),
(4, 'Support');
CREATE OR REPLACE TABLE employees (
id INTEGER,
name VARCHAR,
department_id INTEGER,
manager_id INTEGER
);
INSERT INTO employees VALUES
(1, 'Alice', 1, NULL),
(2, 'Bob', 2, 1),
(3, 'Charlie', 1, 1),
(4, 'Diana', 3, 2),
(5, 'Eve', NULL, 2),
(6, 'Frank', 1, 3);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.
Hint: 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 ORDER BY employee_name
Run your query in the editor and the lesson checks the result automatically.
Lessons in JOIN Types
Combine related tables with INNER, LEFT, RIGHT, FULL OUTER, SELF, and CROSS JOIN, and find rows that have no match.
Related guides
SQL JOINs Explained: INNER, LEFT, FULL OUTER, SELF, and CROSS JOIN
JOINs bring related tables back together: matched rows, missing matches, hierarchies, and all-combination grids all use different JOIN patterns.
SQL Cheat Sheet for Data Analysis: Filter, Group, Join, Dates, and Ranking
One pattern per common analysis question, from previewing a new table to ranking rows inside groups, each linked to a lesson where you can practice it.