SQL • LESSON 22

SELF JOIN

Learn how to join a table to itself to compare related rows.

🔴 Advanced⏱ 3 Minutes⭐ 390 XP
🤔 THE QUESTION

What if we want to combine query results but keep duplicate rows?

💡 WHAT IS IT?

UNION ALL combines the results of two SELECT statements without removing duplicate rows.

🎯 WHAT IS IT USED FOR?

It is useful when every row matters and you do not want SQL to spend time removing duplicates from the combined result.

💻 EXAMPLE
SELECT name
FROM employees
WHERE department = 'IT'

UNION ALL

SELECT name
FROM employees
WHERE department = 'HR';

Mission Brief

Practice using a SELF JOIN to match employees with their managers from the same employees table.

Objectives

  • SELECT
  • FROM
  • SELF JOIN
  • Table aliases
  • LEFT JOIN