Self Join

In this lesson we are going to study about Self joins in a database.

Self Join: 
  • In order to join a table to itself we use the self join.
  • This type of join is used to compare values between two columns in the same table .

Example:-To find the name of Abhishek and Aritra manager we need to


  1. find in EMPLOYEE table by looking the Emp_name columns.
  2. find the manager number from Manager_id.
  3. find the name of manager with manager_id.








sql query :-


select
      emp.Emp_Name as Employee_name,
      manager.Emp_Name as manager_name
from  employees emp,
      employees manager
where emp.manager_id=manager.emp_id



OUTPUT: 


In simulate two table in the from clause there are two  aliases ,namely emp and manager for the same table ,EMPLOYEE.



Related Article
I am interested in hearing your feedback, so that I can improve my articles and learning resources for you.connect with us on facebooktwitter

Share

Did you enjoy reading this and found it useful? If so, please share it with your friends:

4 comments:

  1. use alias in the table a lots of time could been managed look at my blogs--- https://sites.google.com/site/expersqldeveloper

    ReplyDelete
    Replies
    1. Hi
      Thanks for sharing your point on alias .
      I whould like to add few point on why using alias name..

      1.its reduce the code length ,sometime TableName is complex enough to write the same TableName.ColumnName so in place of TableName we use the Table alias Name.
      2.By providing the (FQN) fully Qualified object Name it also reduce the query execution time.

      hope this will help you.
      for more reply.

      Delete
  2. Hey there! Do you know if they make any plugins to assist with SEO?
    I'm trying to get my blog to rank for some
    targeted keywords but I'm not seeing very good success.

    If you know of any please share. Kudos!

    Feel free to visit my page: rowing machine speed

    ReplyDelete
  3. I like the helpful information you provide in your articles.
    I will bookmark your blog and check again here frequently.
    I am quite sure I'll learn a lot of new stuff right here!

    Best of luck for the next!

    Also visit my blog post ... Buy now

    ReplyDelete