You are on page 1of 5

Search Q&A

sign up log in tour help

Questions

Jobs

Documentation Beta

Tags

Users

Badges

Ask Question

x Dismiss

Join the Stack Overflow Community


Stack Overflow is a community of 4.7 million
programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

List all employee's names and their managers by manager name using an inner join
asked 3 years ago
viewed 59537 times
active

26 days ago

The following is my CREATE TABLE script:

create table EMPLOYEES


(EmpID
char(4)
unique Not null,
Ename
varchar(10),
Job
varchar(9),
MGR
char(4),
Hiredate date,
Salary decimal(7,2),
Comm
decimal(7,2),
DeptNo char(2)
not null,
Primary key(EmpID),
Foreign key(DeptNo) REFERENCES DEPARTMENTS(DeptNo));

The following is my INSERT script:


insert
insert
insert
insert
insert
insert
insert
insert
insert
insert
insert
insert
insert
insert

into
into
into
into
into
into
into
into
into
into
into
into
into
into

EMPLOYEES
EMPLOYEES
EMPLOYEES
EMPLOYEES
EMPLOYEES
EMPLOYEES
EMPLOYEES
EMPLOYEES
EMPLOYEES
EMPLOYEES
EMPLOYEES
EMPLOYEES
EMPLOYEES
EMPLOYEES

values
values
values
values
values
values
values
values
values
values
values
values
values
values

(7839,'King','President',null,'17-Nov-11',5000,null,10);
(7698,'Blake','Manager',7839,'01-May-11',2850,null,30);
(7782,'Clark','Manager',7839,'02-Jun-11',2450,null,10);
(7566,'Jones','Manager',7839,'02-Apr-11',2975,null,20);
(7654,'Martin','Salesman',7698,'28-Feb-12',1250,1400,30);
(7499,'Allen','Salesman',7698,'20-Feb-11',1600,300,30);
(7844,'Turner','Salesman',7698,'08-Sep-11',1500,0,30);
(7900,'James','Clerk',7698,'22-Feb-12',950,null,30);
(7521,'Ward','Salesman',7698,'22-Feb-12',1250,500,30);
(7902,'Ford','Analyst',7566,'03-Dec-11',3000,null,20);
(7369,'Smith','Clerk',7902,'17-Dec-10',800,null,20);
(7788,'Scott','Analyst',7566,'09-Dec-12',3000,null,20);
(7876,'Adams','Clerk',7788,'12-Jan-10',1100,null,20);
(7934,'Miller','Clerk',7782,'23-Jan-12',1300,null,10);

The following is my SELECT script:


select distinct e.Ename as Employee, m.mgr as reports_to
from EMPLOYEES e
inner join Employees m on e.mgr = m.mgr;

Im getting the employees with their corresponding manager's ID;

Related
2973

What is the difference between INNER JOIN and


OUTER JOIN?
350

How can I list all foreign keys referencing a given


table in SQL Server?
493

Difference between JOIN and INNER JOIN


535

INNER JOIN ON vs WHERE clause


555

When should I use Cross Apply over Inner Join?


Ford
Scott
Allen
James
Martin
Turner
Ward
Miller
Adams
Blake
Clark
Jones
Smith

7566
7566
7698
7698
7698
7698
7698
7782
7788
7839
7839
7839
7902

143

INNER JOIN vs LEFT JOIN performance in SQL


Server
480

Find all tables containing column with specified


name

converted by W eb2PDFConvert.com

Smith

7902
665

How do I list the manager name as well? *Am I doing the right inner join?*
sql

What's the difference between INNER JOIN, LEFT


JOIN, RIGHT JOIN and FULL JOIN?

sql-server

share improve this question

asked Mar 28 '13 at 9:46


486

Jeff Orris
1,327 3

How to Delete using INNER JOIN with SQL Server?


14

39
-1

8 Answers

active

oldest

votes

How to get all managers from EMP and DEPT table


per deptno (10, 20, 30)?

Hot Network Questions


Add m.Ename to your SELECT query:

Substring Chainification

select distinct e.Ename as Employee, m.mgr as reports_to, m.Ename as Manager


from EMPLOYEES e
inner join Employees m on e.mgr = m.EmpID;

Are there observable changes in a star about to


become supernova, minutes or hours before the
explosion?

share improve this answer

Should I get $200 for passing "Go" if I then get a


card that says "don't collect $200"?

answered Mar 28 '13 at 9:50

Do fitness trackers make you fatter?


How can empty USB sticks contain malware?

Andrey Gordeev
10.3k 3

35

77

Thank you, this was the last script I had to write. Ive been righting sql scripts for the past 5 hours. I can go to
sleep now. I was confusing myself because my brain needs a rest. I would upvote you but I dont have 15
reputation yet. I can accept your answer. VERY HELPFUL! :) Jeff Orris Mar 28 '13 at 10:03

StartsWith in Select Query


Is it "vita" or "vitae"? Why?
Why would the Federation ban escape pods that
come with manual override?
How to request a counter offer in a resignation
letter

@JeffOrris glad it helped. Have a good night Andrey Gordeev Mar 28 '13 at 10:04

How can you possibly stop/kill a person who has


the ability to stop time?
How to get the set of solutions of linear systems
of inequations in Mathematica?
Describing something as 'child like', and not 'a
child'
Why would a country make a declaration of war
instead of just invading?

Your query is close you need to join using the mgr and the empid

Coworker screwed up side-job I hired him for


Is readability a valid reason to NOT using const in
parameters?

on e1.mgr = e2.empid

What is the purpose of women-only meetings,


panels, conferences, etc. in academia?

So the full query is:


select e1.ename Emp,
e2.eName Mgr
from employees e1
inner join employees e2
on e1.mgr = e2.empid

Are Engineered 'Super-Soldiers' a Realistic


Possibility?
How was this scene shot without hurting Charlie
Chaplin?
Should a wall be created as a plane or as a box?

See SQL Fiddle with Demo


If you want to return all rows including those without a manager then you would change it to a LEFT
JOIN (for example the president):

Is there any way to know how much a companion


likes me?
Control the vertical spacing of the parbox relative
to image
Why does "Leidenschaft" mean "passion" while
"leiden" means "to suffer"?

select e1.ename Emp,


e2.eName Mgr
from employees e1
left join employees e2
on e1.mgr = e2.empid

In US museums, why are backpacks only


allowed to be carried on one shoulder?
What is the difference between dynamic and
static electronic correlation

See SQL Fiddle with Demo


The president in your sample data will return a null value for the manager because they do not have
a manager.
share improve this answer

answered Mar 28 '13 at 9:52

bluefeet
146k 33

193

275

You have an incorrect ON clause at the join, this works:

inner join Employees m on e.mgr = m.EmpId;


converted by W eb2PDFConvert.com

inner join Employees m on e.mgr = m.EmpId;

The mgr column references the EmpId column.

DEMO
share improve this answer

answered Mar 28 '13 at 9:50

Tim Schmelter
273k 31

307

489

No, the correct join is:

inner join Employees m on e.mgr = m.EmpID;

You need to match the ManagerID for the current employee with the EmployeeID of the manager. Not
with the ManagerID of the manager.

update
As noted by Andrey Gordeev:
You'd also need to add m.Ename to your SELECT query in order to get the name of the Manager in your
result. Otherwise you'd only get the managerID.
share improve this answer

edited Mar 28 '13 at 9:59

answered Mar 28 '13 at 9:49

Bazzz
14k 7

58

SELECT DISTINCT e.Ename AS Employee,


m.mgr AS reports_to,
m.Ename AS manager
FROM Employees e, Employees m
WHERE e.mgr=m.EmpID;

share improve this answer

27

edited Mar 31 '14 at 9:53

answered Mar 28 '13 at 9:55

Fr0zenFyr

crazyStart

978 8

42 9

26

select e.ename as Employee, m.ename as Manager


from emp e, emp m
where e.mgr = m.empno

If you want to get the result for all the records (irrespective of whether they report to anyone or not),
append (+) on the second table's name
select e.ename as Employee, m.ename as Manager
from emp e, emp m
where e.mgr = m.empno(+)

share improve this answer

edited Feb 16 '15 at 13:19

slavoo
2,741 9

answered Feb 16 '15 at 13:06

Ruchi Gupta
19

29

6 1

Select e.lastname as employee ,m.lastname as manager from employees e,employees m where


e.managerid=m.employyid(+)

share improve this answer

answered Aug 26 '15 at 8:08

Sandeep Desai
11 1

With that all employees with manager and without manger Sandeep Desai Aug 26 '15 at 8:08

converted by W eb2PDFConvert.com

select a.empno,a.ename,a.job,a.mgr,B.empno,B.ename as MGR_name, B.job as MGR_JOB from


emp a, emp B where a.mgr=B.empno ;

0
share improve this answer

edited Aug 27 at 22:08

answered Aug 27 at 21:50

Paul Roub
29.1k 8

shiv kumar prasad


39

63

You may want to tell Jeff how and why this works, and not just post plain code. James T Aug 27 at 22:30

Your Answer

Sign up or log in

Post as a guest

Sign up using Google

Name

Sign up using Facebook

Email

Sign up using Email and Password

required, but never shown

Post Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged sql
question.

sql-server or ask your own

question feed

about us tour help blog chat data legal privacy policy work here advertising info mobile contact us feedback

TECHNOLOGY

LIFE / ARTS

Stack Overflow

Programmers

Server Fault

Unix & Linux

Super User

Ask Different
(Apple)

Web
Applications
Ask Ubuntu
Webmasters
Game
Development
TeX - LaTeX

WordPress
Development
Geographic
Information
Systems
Electrical
Engineering
Android
Enthusiasts

Database
Administrators
Drupal Answers
SharePoint

Code Review

Photography

Academia

Magento

Science Fiction
& Fantasy

more (8)

Signal
Processing

User Experience

Raspberry Pi

Mathematica

Programming
Puzzles & Code
Golf

Salesforce
ExpressionEngine
Answers
Cryptography

more (7)

Graphic
Design
Movies & TV
Music: Practice
& Theory

CULTURE / RECREATION

SCIENCE

English
Language &
Usage

Bicycles

Mathematics

Philosophy

Stack Apps

Cross
Validated
(stats)

more (3)

Meta Stack
Exchange

Skeptics

Roleplaying
Games

Mi Yodeya
(Judaism)

Anime &
Manga

Travel

more (18)

Theoretical
Computer
Science

Christianity

Seasoned
Advice
(cooking)

English
Language
Learners

Home
Improvement

Japanese
Language

OTHER

Area 51
Stack
Overflow
Careers

Physics
MathOverflow
Chemistry
Biology
Computer
Science
converted by W eb2PDFConvert.com

Information
Security

Personal
Finance &
Money

Arqade
(gaming)

site design / logo 2016 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2016.9.22.4005

converted by W eb2PDFConvert.com

You might also like