Professional Documents
Culture Documents
Questions
Jobs
Documentation Beta
Tags
Users
Badges
Ask Question
x Dismiss
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
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);
Related
2973
7566
7566
7698
7698
7698
7698
7698
7782
7788
7839
7839
7839
7902
143
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
sql-server
Jeff Orris
1,327 3
39
-1
8 Answers
active
oldest
votes
Substring Chainification
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
@JeffOrris glad it helped. Have a good night Andrey Gordeev Mar 28 '13 at 10:04
Your query is close you need to join using the mgr and the empid
on e1.mgr = e2.empid
bluefeet
146k 33
193
275
DEMO
share improve this answer
Tim Schmelter
273k 31
307
489
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
Bazzz
14k 7
58
27
Fr0zenFyr
crazyStart
978 8
42 9
26
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(+)
slavoo
2,741 9
Ruchi Gupta
19
29
6 1
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
0
share improve this answer
Paul Roub
29.1k 8
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
Name
Not the answer you're looking for? Browse other questions tagged sql
question.
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
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