XMPP JavaScript Library

SQL SELF JOIN Example

SQL, Sachin Puri, 2012-09-02 13:03:55

SELF-JOIN is similar to any other join with the exception of that in self join we join a table to itself. In other words we treat one table as two different tables and then join them to get desired output.

Example :-

Category table is part of most of the websites, which is used to categorize products or articles in a web site. Some database persons are always confused in creating category, subcategory tables, this articles is for them also.

A category table should look something like following

category_id (PK) category_name category_parent
1 Top 0
2 Sports 1
3 Entertainment 1
4 Football 2
5 TV Series 3
6 Cricket 2
7 Movies 3

 

Now lets create a query to extract category_id, category_name, parent _category_id, parent_category_name

select a.category_id,a.category_name,b.category_id as parent_id,b.category_name as parent_name
from category a join category b
on a.category_parent=b.category_id

 

Result

 

http://www.sachinpuri.com/library/1346574035_large.jpg

 

Add Your Comment
   
    Yes! I want to receive all comments by email

  by bhuwan on 03-Sep-2012 02:28 pm
nice
  • Reply
  •  2 Like
  •  2 Dislike
  • Report