Table of Contents

SQL - Self join

About

A join does not always have to involve two different tables. You can join a table to itself, creating a self-join. Joining a table to itself can be useful when you want to compare values in a column to other values in the same column.

You can:

  • answer practical question such as which products in the Product table have the same names but different types of packaging?
  • or build a hierarchy

Articles Related

Example

table2
(inner and outer set)
Column ID Column ID_2
A
B A
C C
gerardnico@orcl>select t1.id "ID_table2"
  2        FROM table2 t1, table2 t2
  3       WHERE  t1.id = t2.id_2;
 
ID_table2
----------
A
C
  • Bookmark "SQL - Self join" at del.icio.us
  • Bookmark "SQL - Self join" at Digg
  • Bookmark "SQL - Self join" at Ask
  • Bookmark "SQL - Self join" at Google
  • Bookmark "SQL - Self join" at StumbleUpon
  • Bookmark "SQL - Self join" at Technorati
  • Bookmark "SQL - Self join" at Live Bookmarks
  • Bookmark "SQL - Self join" at Yahoo! Myweb
  • Bookmark "SQL - Self join" at Facebook
  • Bookmark "SQL - Self join" at Yahoo! Bookmarks
  • Bookmark "SQL - Self join" at Twitter
  • Bookmark "SQL - Self join" at myAOL
 
language/sql/self_join.txt · Last modified: 2010/08/26 19:56 by gerardnico