From the course: SQL Hands-On Practice: Solve Business Problems
Unlock this course with a free trial
Join today to access over 25,200 courses taught by industry experts.
Introduction to self joins - SQL Tutorial
From the course: SQL Hands-On Practice: Solve Business Problems
Introduction to self joins
- [Instructor] One of the most difficult concepts to learn in SQL are self joins. The good news about self joins is that they work just like regular joins. The syntax and method are exactly the same, so if you're already familiar with joins like left joins and inner joins, you already know the syntax for inner joins. It's the same. What makes self joins difficult is that you're joining a table to itself, which is really conceptually hard to understand. Usually with a self join, you join the primary key of the table to a related foreign key in the same table. In our previous example, we join the primary key of the table, EmployeeID, to the foreign key of the table, ManagerID. Many times, the primary key and foreign key that you're connecting aren't equal, and they often represent hierarchical relationships like employee and manager, or user and admin. The foreign key column can have null values. In the…