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;








-1















Data Table
enter image description here



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:
enter image description here



Expected Output:
enter image description here



Absolute beginner in SQL and new to stackoverflow, help rendered will be greatly appreciated










share|improve this question
























  • 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 * FROM staff s where exists(select 1 from staff x where x.supervisor_id = s.id);

    – joop
    Nov 15 '18 at 14:55

















-1















Data Table
enter image description here



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:
enter image description here



Expected Output:
enter image description here



Absolute beginner in SQL and new to stackoverflow, help rendered will be greatly appreciated










share|improve this question
























  • 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 * FROM staff s where exists(select 1 from staff x where x.supervisor_id = s.id);

    – joop
    Nov 15 '18 at 14:55













-1












-1








-1








Data Table
enter image description here



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:
enter image description here



Expected Output:
enter image description here



Absolute beginner in SQL and new to stackoverflow, help rendered will be greatly appreciated










share|improve this question
















Data Table
enter image description here



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:
enter image description here



Expected Output:
enter image description here



Absolute beginner in SQL and new to stackoverflow, help rendered will be greatly appreciated







sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:52









Eray Balkanli

4,65852347




4,65852347










asked Nov 15 '18 at 14:43









user9978150user9978150

12




12












  • 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 * 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











  • 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












3 Answers
3






active

oldest

votes


















0














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





share|improve this answer






























    2














    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.






    share|improve this answer






























      0














      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





      share|improve this answer























        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
        );



        );













        draft saved

        draft discarded


















        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









        0














        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





        share|improve this answer



























          0














          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





          share|improve this answer

























            0












            0








            0







            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





            share|improve this answer













            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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 14:47









            Eray BalkanliEray Balkanli

            4,65852347




            4,65852347























                2














                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.






                share|improve this answer



























                  2














                  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.






                  share|improve this answer

























                    2












                    2








                    2







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 14:56









                    Gordon LinoffGordon Linoff

                    798k37320426




                    798k37320426





















                        0














                        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





                        share|improve this answer



























                          0














                          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





                          share|improve this answer

























                            0












                            0








                            0







                            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





                            share|improve this answer













                            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






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 15 '18 at 14:53









                            D. HurtadoD. Hurtado

                            966




                            966



























                                draft saved

                                draft discarded
















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                Syphilis

                                Darth Vader #20