SELF JOIN in SQL: Joining a Table with Itself

#Day_34 of learning SQL in 60 days Topic I covered: A SELF JOIN is when a table is joined with itself. It might sound confusing at first, but it’s extremely useful when working with hierarchical data. When do we use SELF JOIN? When a table contains related data within itself. Example: Employees and their Managers are stored in the same table. How it works: We use table aliases to treat the same table as two different tables. Syntax: SELECT A.COLUMN_NAME, B.COLUMN_NAME FROM TABLE_NAME A JOIN TABLE_NAME B ON A.COMMON_COLUMN = B.COMMON_COLUMN; Example: select e.emp_name as employe,m.emp_name as manager from employees e join employees m on e.manager_id=m.emp_id; Here: E represents Employees M represents Managers (same table!) Key Takeaways: ✔ SELF JOIN joins a table to itself ✔ Requires aliases to differentiate roles ✔ Useful for hierarchical relationships This concept really helped me understand how relational databases handle real-world relationships within the same dataset. #SQL #MySQL #Database #LearningJourney #TechSkills

  • graphical user interface, text, application, email

To view or add a comment, sign in

Explore content categories