ChatGPT解决这个技术问题 Extra ChatGPT

What is SELF JOIN and when would you use it? [duplicate]

This question already has answers here: Explanation of self-joins (13 answers) Closed 2 years ago.

What is self join and when would you use it? I don't understand self joins so a layman explanation with an example would be great.


D
D'Arcy Rittich

You use a self join when a table references data in itself.

E.g., an Employee table may have a SupervisorID column that points to the employee that is the boss of the current employee.

To query the data and get information for both people in one row, you could self join like this:

select e1.EmployeeID, 
    e1.FirstName, 
    e1.LastName,
    e1.SupervisorID, 
    e2.FirstName as SupervisorFirstName, 
    e2.LastName as SupervisorLastName
from Employee e1
left outer join Employee e2 on e1.SupervisorID = e2.EmployeeID

That looks like a self outer join to me?
@JoeCaruso That's because it is ;)
I wonder why noone emphasizes the "in the same row" part. Isn't that whole point of doing a self join?
don't we need AS when creating alias for tables?
@ManuChadha AS is optional - I will decide based on the readability of the query.
p
paxdiablo

Well, one classic example is where you wanted to get a list of employees and their immediate managers:

select e.employee as employee, b.employee as boss
from emptable e, emptable b
where e.manager_id = b.empolyee_id
order by 1

It's basically used where there is any relationship between rows stored in the same table.

employees.

multi-level marketing.

machine parts.

And so on...


Boo for non-ANSI syntax and using ordinal in the ORDER BY clause rather than a column name.
Boo right back at you for missing the point of the answer :-) I tend to use the simplest syntax that works since the good DBMS' won't let that affect the performance.
best answer so far... :)
M
Mark Byers

A self join is simply when you join a table with itself. There is no SELF JOIN keyword, you just write an ordinary join where both tables involved in the join are the same table. One thing to notice is that when you are self joining it is necessary to use an alias for the table otherwise the table name would be ambiguous.

It is useful when you want to correlate pairs of rows from the same table, for example a parent - child relationship. The following query returns the names of all immediate subcategories of the category 'Kitchen'.

SELECT T2.name
FROM category T1
JOIN category T2
ON T2.parent = T1.id
WHERE T1.name = 'Kitchen'

Nice to see this comment.. ON T2.parent = T1.id (because it affects result if we interchange 'parent' and 'id' in it)
Good to mention there is no SELF JOIN keyword. I was confused about that part!
M
Michael Pakhantsov

SQL self-join simply is a normal join which is used to join a table to itself.

Example:

Select *
FROM Table t1, Table t2
WHERE t1.Id = t2.ID

Wouldn't it be safer to use SELECT t1.* in order to avoid duplicate column names in the result?
W
Will A

You'd use a self-join on a table that "refers" to itself - e.g. a table of employees where managerid is a foreign-key to employeeid on that same table.

Example:

SELECT E.name, ME.name AS manager
FROM dbo.Employees E
LEFT JOIN dbo.Employees ME
ON ME.employeeid = E.managerid

ON ME.employeeid = E.managerid will create different result instead of ON ME.managerid = E.employeeid