课程: SQL Hands-On Practice: Solve Business Problems
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
Introduction to self joins - SQL教程
课程: 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…