
3. Filtering self joined tables
Dictionary database


Great! When self-joining tables, we can still filter the results with the WHERE clause, just as we can when JOINing two different tables.

Suppose that we want to show each employee's name along with the name of their supervisor for only those employees who have less than five years of experience:

  e.name AS employee_name,
  s.name AS supervisor_name
FROM employee AS e
JOIN employee AS s
  ON e.supervisor_id = s.id
WHERE e.experience < 5

Note that we always have to specify the table name (an alias) before a given column when self-joining. Otherwise, the column name would be ambiguous, as there would be two columns with the same name: one from the employee table, and one from the supervisor table.


Show only worker's name, and specialization. Alias column with the name as apprentice. Consider only those workers whose supervisors have more than 14 years of experience.

Stuck? Here's a hint!


  apprentice.name apprentice,
FROM workshop_workers apprentice
JOIN workshop_workers master
  ON apprentice.master_id = master.id
WHERE master.experience > 14