What is a self-join in SQL?
a) A join in which a table is joined with itself
b) A join that uses only one column
c) A join between two unrelated tables
d) A join that combines all columns of two tables
Answer:
a) A join in which a table is joined with itself
Explanation:
A self-join is a join in which a table is joined with itself. It is typically used to compare rows within the same table or to retrieve hierarchical data, such as an organizational structure where employees report to other employees.
In a self-join, an alias is used to give the table two different names within the same query. This allows you to reference the same table as though it were two separate tables.
Self-joins are useful for solving problems that require comparison within a single table, such as finding employees who work in the same department or finding duplicates in a dataset.