Chapter 6 · Lesson 3 of 7
LEFT JOIN Keeps the Left Side
Keep every row from the left table with LEFT JOIN, see unmatched rows filled with NULL, and list departments even without employees.
A LEFT JOIN keeps every row from the left table and fills missing right-side values with NULL.
SELECT d.name AS department_name, e.name AS employee_name
FROM departments AS d
LEFT JOIN employees AS e
ON d.id = e.department_idThis is useful when you want to keep all rows from one table, even if some of them do not have a match in the other table.
In this example, Support still appears even though nobody works there.
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
Show every department and any employee assigned to it. Return department_name and employee_name, ordered by department_name and then employee_name. Make sure Support still appears even though nobody works there.
Hint: Start from departments, then LEFT JOIN employees ON departments.id = employees.department_id.
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.
Analyzing Sensitive Data in the Browser: What Stays Local and a Safety Checklist
Understand the local processing model behind SQL for Files and how to work safely with sensitive CSV, JSON, and Parquet files.