Q
Problem solve Get help with specific problems with your technologies, process and projects.

Select empname and managername

What is the query to select empname and his managername from an emp table?

What is the query to select empname and his managername from an emp table?

This sounds like another homework question. However, it is also probably the most common example of a self-join ever devised. Everybody uses this example, and it's real easy to find on the Web, so there's no reason why TechTarget shouldn't also publish it.

select e.empname
     , m.empname as managername
  from emp as e
left outer
  join emp as m
    on m.empid = e.managerid

Whenever you join a table to itself, use table aliases to distinguish the "copies" of the table. (The optimizer might not actually use two "copies" of the table, but it is convenient to think of it as working that way.)

This was last published in January 2006

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close