Database Tutorial

Course Tutorial Site

Site Admin

Self Join

without comments

Three Table Self Join

A three table self join involves the join between two tables into a result set, and a second join of the result set to a third table. The join between the first two tables or the join between the result set and a third can use any of these join operators: INNER, LEFT, RIGHT, or FULL JOIN.

A table supports three table self joins when it contains a primary key and two foreign key columns. The system_user table in our video store model meets that criteria because it has:

  • A primary key column – system_user_id
  • Two foreign key columns: created_by and last_updated_by

A three table self join requires that:

  • You need to join the created_by foreign key column from the first copy of a table to the system_user_id primary key column of second copy of the table; and
  • You need to join the last_updated_by foreign key column from the first copy of a table to the system_user_id primary key column of third copy of the table.

The following illustrates the behavior of a three table self join of the system_user table:

selfreference3table

The diagram shows:

  • How the first system_user copy’s created_by foreign key column points to the second system_user copy’s system_user_id primary key column.
  • How the first system_user copy’s last_updated_by foreign key column points to the third system_user copy’s system_user_id primary key column.

Written by michaelmclaughlin

August 13th, 2018 at 1:25 pm

Posted in