Joining tables: INNER, LEFT and self joins
Good databases split data across tables, and joins bring it back together. The most useful trick here is finding what's missing, with a LEFT JOIN.
● A real database, running in your browser. Nothing you type leaves this page.
Exercise 1 of 6
Two tables
JOIN … ON matches rows from two tables. Short names after each table, such as e and d, keep it readable.
Your task
Show each employee's first_name, with their department's name.
Tables and columns
or Ctrl+Enter
What you'll practice
- Two tables.
JOIN … ONmatches rows from two tables. Short names after each table, such aseandd, keep it readable. - Filter a join. After the join, you can use
WHEREon columns from either table. - Join and count. Joins and grouping work together, and now you can show names rather than ids.
- Find what's missing. A
LEFT JOINkeeps every row of the left table, withNULLwhere there's no match. SoWHERE right.id IS NULLfinds rows with no match at all. - Nobody on a project. It's the same idea, on a table that links two others.
- A table with itself. A manager is also an employee, so
employeesjoins to itself, with two short names. ALEFT JOINkeeps those who have no manager.