views:

302

answers:

8

Basically when do we need a self join on a table?

+1  A: 

Here is an example.

A self join is commonly used when you have a table with dates and you want to compare one date to another within the same table, by extension, when you compare values of a field to a subset of values from the same field in the table. An article discussing this in the context of an Oracle database can be found here.

luvieere
+2  A: 

When you have to join two records in the same table.

mck89
or even just one record to itself...
Jeffrey Kemp
+6  A: 

Whe you have child / parent relations in the table.

CREATE TABLE Users(
  UserID INT,
  FName VARCHAR(50),
  LName VARCHAR(50),
  ManagerID INT
)

SELECT  u.UserID,
     u.FName,
     u.LName,
     um.ManagerID,
     um.FName ManagerFName,
     um.LName ManagerLName
FROM    Users u INNER JOIN
     Users um ON u.ManagerID = um.UserID
astander
+1  A: 

One situation would be when using hierarchical data organised in an adjacency list. See something like this for further details.

A_M
+1  A: 

Use a self-join to simplify nested SQL queries where the inner and outer queries reference the same table. Self joins are often used in subqueries.Sometimes Group By can be used to avoid self join.

This explains how subqueries and self-join relates http://www.firstsql.com/tutor3.htm#self

valli
can you elaborate on this? I would have thought self joins and subqueries were orthogonal. Unless you were thinking of queries that perform self-joins to find minima/maxima or other types of analytics?
Jeffrey Kemp
A: 

self joins are common in (arguably poorly-designed) tables that store more than one entity type, e.g. in a typical EAV (Entity-Attribute-Value) design, queries will tend to join to the same table many times.

In a system I'm currently working with, the majority of attributes aren't stored together in single rows at all; instead, the designers created a series of "property" tables, each with a structure like (ID, AttributeName, AttributeValue, StartDate, EndDate) - which means that typical queries will join these property tables once for each attribute needed. Not the most efficient design for batch processing and reporting, believe me!

Jeffrey Kemp
1) Although one could in some cases alter self-referencing designs to separate entities into their own tables (in which the pros and cons of the design could be debated), this is not always possible; as for example hierarchical information. 2) Not to mention that some self-join queries aren't a result of the design, they a result of the rules required to access the desired data. A classic is finding gaps (of whatever form).
Craig Young
(1) yes. And a hierarchical model would usually imply that you're only talking about a single entity, so of course it should be in one table. (2) yes, my answer describes just one of many uses of self-joins.
Jeffrey Kemp
+1  A: 

Here are a few reasons off the top of my head:

  • The table design is self-referencing.
    • This is typical of hierarchical structures such as employee -> manager relationships.
    • Double-entry transaction systems may store both legs of a transaction in the same table.
  • Sometimes self joins are required purely as a result of the information required - having nothing to do with the actual design.
Craig Young
A: 

I just used it to store and display a hierarchical menu system for a CMS without going all drupally and storing everything in some sort of JSON string thingy.

actionAxolot