0
0
MySQLquery~5 mins

Self JOIN in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Self JOIN in SQL?
A Self JOIN is when a table is joined with itself. It helps compare rows within the same table by treating it as if there were two tables.
Click to reveal answer
beginner
Why would you use a Self JOIN?
You use a Self JOIN to find relationships between rows in the same table, like finding an employee's manager in an employee table.
Click to reveal answer
beginner
In a Self JOIN, why do we use table aliases?
Table aliases give the same table different names so SQL can tell which copy of the table you mean when comparing rows.
Click to reveal answer
intermediate
Write a simple Self JOIN query to find employees and their managers from an employee table with columns (id, name, manager_id).
SELECT e.name AS Employee, m.name AS Manager FROM employee e LEFT JOIN employee m ON e.manager_id = m.id;
Click to reveal answer
beginner
What happens if you forget to use aliases in a Self JOIN?
SQL will get confused because it can't tell which instance of the table you mean, causing errors or unexpected results.
Click to reveal answer
What does a Self JOIN do?
AJoins two different tables
BJoins tables with no common columns
CJoins a table with itself
DJoins tables only on primary keys
Why are aliases important in a Self JOIN?
ATo give the same table different names for clarity
BTo avoid using WHERE clause
CTo speed up the query
DTo rename columns
In a Self JOIN, if you want to find an employee's manager, which column would you join on?
Aemployee.id = manager.id
Bemployee.manager_id = manager.id
Cemployee.name = manager.name
Demployee.id = manager.manager_id
What type of JOIN is commonly used in Self JOIN to include employees without managers?
ALEFT JOIN
BRIGHT JOIN
CINNER JOIN
DFULL JOIN
Which of these is a valid alias usage in a Self JOIN?
AFROM employee AS e JOIN employee AS e ON e.manager_id = e.id
BFROM employee JOIN employee ON employee.manager_id = employee.id
CFROM employee JOIN employee
DFROM employee e JOIN employee m ON e.manager_id = m.id
Explain what a Self JOIN is and give a simple example scenario where it is useful.
Think about how one table can relate to itself.
You got /3 concepts.
    Describe the role of table aliases in a Self JOIN and what problems they solve.
    Why can't you just write the table name twice without aliases?
    You got /3 concepts.