Joins · SQL SQL
Which of the following queries demonstrates a self-join?
Practice question from Joins (SQL SQL), with the correct answer and a step-by-step solution.
medium · MCQ · Q-25A2A0
Which of the following queries demonstrates a self-join?
- A.SELECT * FROM employees JOIN departments ON employees.dept_id = departments.id;
- B.SELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.id;Correct answer
- C.SELECT * FROM employees CROSS JOIN contractors;
- D.SELECT * FROM employees NATURAL JOIN salaries;
Solution
A self-join is a regular join where a table is joined to itself, typically using table aliases (like e1 and e2) to distinguish between the two instances of the table, often used for hierarchical data like employee-manager relationships.
More from Joins
- What is the primary difference between an INNER JOIN and a LEFT OUTER JOIN in ANSI SQL?
- Consider two tables: A with 5 rows and B with 4 rows. If you perform a CROSS JOIN between A and B without any WHERE clause, how many rows w…
- What will be the behavior of a FULL OUTER JOIN between Table X and Table Y if there are rows in Table Y that do not match any rows in Table…
- Explain the semantic difference between placing a condition in the ON clause versus the WHERE clause when performing a LEFT OUTER JOIN.
Practise more SQL
Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.
Start practising freeFree to start · No credit card needed