SQL Question: INNER JOIN(SELF JOIN) eliminates duplicate values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Data Table
Question: List the names of all the supervisors in alphabetical order. If the supervisor has more than one staff, his name should appear only once.
My input and output:
Expected Output:
Absolute beginner in SQL and new to stackoverflow, help rendered will be greatly appreciated
sql sql-server
add a comment |
Data Table
Question: List the names of all the supervisors in alphabetical order. If the supervisor has more than one staff, his name should appear only once.
My input and output:
Expected Output:
Absolute beginner in SQL and new to stackoverflow, help rendered will be greatly appreciated
sql sql-server
UseLEFT JOIN
instead ofINNER JOIN
– Jean-Marc Zimmer
Nov 15 '18 at 14:45
select distinct supervisorname from ...
– jarlh
Nov 15 '18 at 14:46
SELECT * FROM staff s where exists(select 1 from staff x where x.supervisor_id = s.id);
– joop
Nov 15 '18 at 14:55
add a comment |
Data Table
Question: List the names of all the supervisors in alphabetical order. If the supervisor has more than one staff, his name should appear only once.
My input and output:
Expected Output:
Absolute beginner in SQL and new to stackoverflow, help rendered will be greatly appreciated
sql sql-server
Data Table
Question: List the names of all the supervisors in alphabetical order. If the supervisor has more than one staff, his name should appear only once.
My input and output:
Expected Output:
Absolute beginner in SQL and new to stackoverflow, help rendered will be greatly appreciated
sql sql-server
sql sql-server
edited Nov 15 '18 at 14:52
Eray Balkanli
4,65852347
4,65852347
asked Nov 15 '18 at 14:43
user9978150user9978150
12
12
UseLEFT JOIN
instead ofINNER JOIN
– Jean-Marc Zimmer
Nov 15 '18 at 14:45
select distinct supervisorname from ...
– jarlh
Nov 15 '18 at 14:46
SELECT * FROM staff s where exists(select 1 from staff x where x.supervisor_id = s.id);
– joop
Nov 15 '18 at 14:55
add a comment |
UseLEFT JOIN
instead ofINNER JOIN
– Jean-Marc Zimmer
Nov 15 '18 at 14:45
select distinct supervisorname from ...
– jarlh
Nov 15 '18 at 14:46
SELECT * FROM staff s where exists(select 1 from staff x where x.supervisor_id = s.id);
– joop
Nov 15 '18 at 14:55
Use
LEFT JOIN
instead of INNER JOIN
– Jean-Marc Zimmer
Nov 15 '18 at 14:45
Use
LEFT JOIN
instead of INNER JOIN
– Jean-Marc Zimmer
Nov 15 '18 at 14:45
select distinct supervisorname from ...
– jarlh
Nov 15 '18 at 14:46
select distinct supervisorname from ...
– jarlh
Nov 15 '18 at 14:46
SELECT * FROM staff s where exists(select 1 from staff x where x.supervisor_id = s.id);
– joop
Nov 15 '18 at 14:55
SELECT * FROM staff s where exists(select 1 from staff x where x.supervisor_id = s.id);
– joop
Nov 15 '18 at 14:55
add a comment |
3 Answers
3
active
oldest
votes
I think you need to find out the supervisors first by using a cte or temptable, then use it to select the names like:
;with cte as ( --supervisors
select distinct s1.Id
from Staff s1
inner join Staff s2 on s1.Id=s2.SupervisorId
)
select s.Name
from Staff s
inner join cte on s.Id = cte.Id
order by s.Name
add a comment |
You can simply do:
select s.*
from staff s
where exists (select 1 from staff s2 where s.staffId = s2.supervisorid);
That is, select all staff who are supervisors.
add a comment |
You could try this query:
SELECT MAX(Staff.Name) As SupervisorName
FROM Staff INNER JOIN Staff Sup
ON Staff.SupervisorID = Sup.StaffID
GROUP BY Staff.Name
ORDER BY Staff.Name ASC
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321943%2fsql-question-inner-joinself-join-eliminates-duplicate-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you need to find out the supervisors first by using a cte or temptable, then use it to select the names like:
;with cte as ( --supervisors
select distinct s1.Id
from Staff s1
inner join Staff s2 on s1.Id=s2.SupervisorId
)
select s.Name
from Staff s
inner join cte on s.Id = cte.Id
order by s.Name
add a comment |
I think you need to find out the supervisors first by using a cte or temptable, then use it to select the names like:
;with cte as ( --supervisors
select distinct s1.Id
from Staff s1
inner join Staff s2 on s1.Id=s2.SupervisorId
)
select s.Name
from Staff s
inner join cte on s.Id = cte.Id
order by s.Name
add a comment |
I think you need to find out the supervisors first by using a cte or temptable, then use it to select the names like:
;with cte as ( --supervisors
select distinct s1.Id
from Staff s1
inner join Staff s2 on s1.Id=s2.SupervisorId
)
select s.Name
from Staff s
inner join cte on s.Id = cte.Id
order by s.Name
I think you need to find out the supervisors first by using a cte or temptable, then use it to select the names like:
;with cte as ( --supervisors
select distinct s1.Id
from Staff s1
inner join Staff s2 on s1.Id=s2.SupervisorId
)
select s.Name
from Staff s
inner join cte on s.Id = cte.Id
order by s.Name
answered Nov 15 '18 at 14:47
Eray BalkanliEray Balkanli
4,65852347
4,65852347
add a comment |
add a comment |
You can simply do:
select s.*
from staff s
where exists (select 1 from staff s2 where s.staffId = s2.supervisorid);
That is, select all staff who are supervisors.
add a comment |
You can simply do:
select s.*
from staff s
where exists (select 1 from staff s2 where s.staffId = s2.supervisorid);
That is, select all staff who are supervisors.
add a comment |
You can simply do:
select s.*
from staff s
where exists (select 1 from staff s2 where s.staffId = s2.supervisorid);
That is, select all staff who are supervisors.
You can simply do:
select s.*
from staff s
where exists (select 1 from staff s2 where s.staffId = s2.supervisorid);
That is, select all staff who are supervisors.
answered Nov 15 '18 at 14:56
Gordon LinoffGordon Linoff
798k37320426
798k37320426
add a comment |
add a comment |
You could try this query:
SELECT MAX(Staff.Name) As SupervisorName
FROM Staff INNER JOIN Staff Sup
ON Staff.SupervisorID = Sup.StaffID
GROUP BY Staff.Name
ORDER BY Staff.Name ASC
add a comment |
You could try this query:
SELECT MAX(Staff.Name) As SupervisorName
FROM Staff INNER JOIN Staff Sup
ON Staff.SupervisorID = Sup.StaffID
GROUP BY Staff.Name
ORDER BY Staff.Name ASC
add a comment |
You could try this query:
SELECT MAX(Staff.Name) As SupervisorName
FROM Staff INNER JOIN Staff Sup
ON Staff.SupervisorID = Sup.StaffID
GROUP BY Staff.Name
ORDER BY Staff.Name ASC
You could try this query:
SELECT MAX(Staff.Name) As SupervisorName
FROM Staff INNER JOIN Staff Sup
ON Staff.SupervisorID = Sup.StaffID
GROUP BY Staff.Name
ORDER BY Staff.Name ASC
answered Nov 15 '18 at 14:53
D. HurtadoD. Hurtado
966
966
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321943%2fsql-question-inner-joinself-join-eliminates-duplicate-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Use
LEFT JOIN
instead ofINNER JOIN
– Jean-Marc Zimmer
Nov 15 '18 at 14:45
select distinct supervisorname from ...
– jarlh
Nov 15 '18 at 14:46
SELECT * FROM staff s where exists(select 1 from staff x where x.supervisor_id = s.id);
– joop
Nov 15 '18 at 14:55