A self join can be an INNER, LEFT, RIGHT, or FULL JOIN. A table supports a self join when it contains a PRIMARY KEY and FOREIGN KEY that points to it’s own PRIMARY KEY. This means you can join a row in the table to the same row or another row in the table, and the join is wholly dependent on the matching of values in the PRIMARY and FOREIGN KEY column(s).
A self join acts like a nested loop where you match a key from the outer loop against a key from the inner loop. The trick is that the outer and inner loop hold different copies of the same data set. You effectively tie together two rows into one master set of information, much as you would rows from different tables in any other join operation.
Self Join Example ↓
Rule(s):
- A self join must have a join statement.
- A self join depends on table aliases to make copies of the same source table, and table aliases are critical writing self joins.
- A self join depends on table aliases to disambiguate columns that share the same name in different tables.
- A self join may use an an
INNER, LEFT, RIGHT, or FULL JOIN clause must include a join statement in the FROM clause.
ANSI SQL:89
The following syntax examples let you perform a self join of two copies of one table in any database.
ANSI SQL:89 syntax does support an INNER JOIN. An INNER JOIN is an equijoin or filtered Cartesian Product. The filter matches values in one set with another based on equal values in one or more columns of each copy of the table in a self join.
1
2
3
4
| SELECT su1.system_user
, su2.system_user
FROM system_user su1, system_user su2
WHERE su1.created_by = su2.system_user_id; |
This returns the SYSTEM_USER column values for the primary row and the SYSTEM_USER column value for the row that created the row. It works because of a FOREIGN KEY column that points to a PRIMARY KEY column in the same table. Naturally, in this case the PRIMARY and FOREIGN KEY have different column names. All column names are unique in any table defintion.
ANSI SQL:92
The following syntax examples let you perform a self join of two copies of one table in any database.
ANSI SQL:89 syntax does support an INNER JOIN. An INNER JOIN is an equijoin or filtered Cartesian Product. The filter matches values in one set with another based on equal values in one or more columns of each copy of the table in a self join.
1
2
3
4
| SELECT su1.system_user
, su2.system_user
FROM system_user su1 INNER JOIN system_user su2
ON su1.created_by = su2.system_user_id; |
This returns the SYSTEM_USER column values for the primary row and the SYSTEM_USER column value for the row that created the row. It works because of a FOREIGN KEY column that points to a PRIMARY KEY column in the same table. Naturally, in this case the PRIMARY and FOREIGN KEY have different column names. All column names are unique in any table defintion.
I believe I understand the concept and how to implement a self join, but is there an example of a ‘real-world’ situation that would call for a self join? I’m just drawing a blank as to when I would use this.
Josiah
5 Feb 10 at 12:50 pm
Sure, in this example you’re discovering who gave access to somebody. It’s done by leveraging the “who-audit” columns of
CREATED_BYandLAST_UPDATED_BY. These two columns can be tied to the same or different rows in theSYSTEM_USERtable. Any attempt to find both the creator or last updator of a row in the table requires a self-join to link the two rows together.You can also check the hierarchical join mechanics for an organization to see another concrete example.
michaelmclaughlin
5 Feb 10 at 1:03 pm
Thanks.
Josiah
5 Feb 10 at 1:42 pm