SQL INTERVIEW PREPARATION PART-11

What is a self join in SQL? Provide an example.

A self join is a regular join but the table is joined with itself. This is useful when the data is hierarchical or when you need to compare rows within the same table.

Example:
SELECT e1.name AS Employee, e2.name AS Manager
FROM employees e1
JOIN employees e2 ON e1.manager_id = e2.id;
In this example, the employees table is joined with itself to find the manager for each employee.

Tip: Explain that self joins are particularly useful for hierarchical data, such as organizational charts, and emphasize the importance of using table aliases (e.g., e1 and e2) to differentiate between the different instances of the same table.

Share with credits: https://t.me/sqlspecialist

Like this post if you want me to continue SQL Interview Preparation Series 👍❤️

Hope it helps :)

--

--

Data Analytics
Mr. Plan ₿ Publication

Data Science, SQL, Excel, Python, Power BI, Tableau & Machine Learning Best Resources: heylink.me/DataAnalytics