How to find least group of students












-1














I have this tables:



Student - Id, FirstName, LastName, Age
Group - Id, Name
Student_Group - Student_Id, Group_Id


I need to find least group of student. I have tried many times. I would be so glad if somebody helped.










share|improve this question






















  • Add some sample table data and the expected result - all as formatted text, not images. Also show us your best query attempt.
    – jarlh
    Nov 14 '18 at 13:12










  • What exactly would be the least group?
    – Salman A
    Nov 14 '18 at 13:13










  • What is your definition of "least"? Fewest members? Youngest age?
    – DavidG
    Nov 14 '18 at 13:13












  • Fewest group as student count. My English very bad. That's why I'm sorry
    – Nasirli01
    Nov 14 '18 at 13:20










  • Could you provide that sample data and expected results that Jarlh asked for, please?
    – Larnu
    Nov 14 '18 at 13:23
















-1














I have this tables:



Student - Id, FirstName, LastName, Age
Group - Id, Name
Student_Group - Student_Id, Group_Id


I need to find least group of student. I have tried many times. I would be so glad if somebody helped.










share|improve this question






















  • Add some sample table data and the expected result - all as formatted text, not images. Also show us your best query attempt.
    – jarlh
    Nov 14 '18 at 13:12










  • What exactly would be the least group?
    – Salman A
    Nov 14 '18 at 13:13










  • What is your definition of "least"? Fewest members? Youngest age?
    – DavidG
    Nov 14 '18 at 13:13












  • Fewest group as student count. My English very bad. That's why I'm sorry
    – Nasirli01
    Nov 14 '18 at 13:20










  • Could you provide that sample data and expected results that Jarlh asked for, please?
    – Larnu
    Nov 14 '18 at 13:23














-1












-1








-1







I have this tables:



Student - Id, FirstName, LastName, Age
Group - Id, Name
Student_Group - Student_Id, Group_Id


I need to find least group of student. I have tried many times. I would be so glad if somebody helped.










share|improve this question













I have this tables:



Student - Id, FirstName, LastName, Age
Group - Id, Name
Student_Group - Student_Id, Group_Id


I need to find least group of student. I have tried many times. I would be so glad if somebody helped.







sql sql-server join many-to-many






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 13:10









Nasirli01

236




236












  • Add some sample table data and the expected result - all as formatted text, not images. Also show us your best query attempt.
    – jarlh
    Nov 14 '18 at 13:12










  • What exactly would be the least group?
    – Salman A
    Nov 14 '18 at 13:13










  • What is your definition of "least"? Fewest members? Youngest age?
    – DavidG
    Nov 14 '18 at 13:13












  • Fewest group as student count. My English very bad. That's why I'm sorry
    – Nasirli01
    Nov 14 '18 at 13:20










  • Could you provide that sample data and expected results that Jarlh asked for, please?
    – Larnu
    Nov 14 '18 at 13:23


















  • Add some sample table data and the expected result - all as formatted text, not images. Also show us your best query attempt.
    – jarlh
    Nov 14 '18 at 13:12










  • What exactly would be the least group?
    – Salman A
    Nov 14 '18 at 13:13










  • What is your definition of "least"? Fewest members? Youngest age?
    – DavidG
    Nov 14 '18 at 13:13












  • Fewest group as student count. My English very bad. That's why I'm sorry
    – Nasirli01
    Nov 14 '18 at 13:20










  • Could you provide that sample data and expected results that Jarlh asked for, please?
    – Larnu
    Nov 14 '18 at 13:23
















Add some sample table data and the expected result - all as formatted text, not images. Also show us your best query attempt.
– jarlh
Nov 14 '18 at 13:12




Add some sample table data and the expected result - all as formatted text, not images. Also show us your best query attempt.
– jarlh
Nov 14 '18 at 13:12












What exactly would be the least group?
– Salman A
Nov 14 '18 at 13:13




What exactly would be the least group?
– Salman A
Nov 14 '18 at 13:13












What is your definition of "least"? Fewest members? Youngest age?
– DavidG
Nov 14 '18 at 13:13






What is your definition of "least"? Fewest members? Youngest age?
– DavidG
Nov 14 '18 at 13:13














Fewest group as student count. My English very bad. That's why I'm sorry
– Nasirli01
Nov 14 '18 at 13:20




Fewest group as student count. My English very bad. That's why I'm sorry
– Nasirli01
Nov 14 '18 at 13:20












Could you provide that sample data and expected results that Jarlh asked for, please?
– Larnu
Nov 14 '18 at 13:23




Could you provide that sample data and expected results that Jarlh asked for, please?
– Larnu
Nov 14 '18 at 13:23












2 Answers
2






active

oldest

votes


















2














Have you tried to do



SELECT top 1 g.name
FROM group g INNER JOIN Student_group sg ON g.id = sg.Group_Id
WHERE count(sg.student_id) >0
ORDER BY count(sg.student_id)
GROUP BY sg.group_id


?



If you want also the groups with 0 students you should to do



SELECT top 1 g.name
FROM group g INNER JOIN Student_group sg ON g.id = sg.Group_Id
ORDER BY count(sg.student_id)
GROUP BY sg.group_id





share|improve this answer































    2














    --If you just need the group with the least members,
    --Group By and Count will work to find the Group with the least members
    --Then use select top 1 record and order by GroupCount Ascending
    SELECT TOP 1 Group_Id, COUNT(Group_Id) AS [GroupCount]
    FROM Student_Group
    GROUP BY Group_Id
    ORDER BY [GroupCount]





    share|improve this answer

















    • 1




      How can you possibly answer this question when we have no idea what "least" is?
      – DavidG
      Nov 14 '18 at 13:16










    • I think "least" is pretty self-explanatory
      – Ryan Wilson
      Nov 14 '18 at 13:16










    • That's not what the question asked, it asked "I need to find least group of student. "
      – Ryan Wilson
      Nov 14 '18 at 13:17










    • @DavidG No where in the post does it say that.
      – Ryan Wilson
      Nov 14 '18 at 13:18










    • @DavidG I based my answer off of what the post does ask for, not some imaginary scenario Oh, look, the OP just verified my answer
      – Ryan Wilson
      Nov 14 '18 at 13:19













    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%2f53301034%2fhow-to-find-least-group-of-students%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Have you tried to do



    SELECT top 1 g.name
    FROM group g INNER JOIN Student_group sg ON g.id = sg.Group_Id
    WHERE count(sg.student_id) >0
    ORDER BY count(sg.student_id)
    GROUP BY sg.group_id


    ?



    If you want also the groups with 0 students you should to do



    SELECT top 1 g.name
    FROM group g INNER JOIN Student_group sg ON g.id = sg.Group_Id
    ORDER BY count(sg.student_id)
    GROUP BY sg.group_id





    share|improve this answer




























      2














      Have you tried to do



      SELECT top 1 g.name
      FROM group g INNER JOIN Student_group sg ON g.id = sg.Group_Id
      WHERE count(sg.student_id) >0
      ORDER BY count(sg.student_id)
      GROUP BY sg.group_id


      ?



      If you want also the groups with 0 students you should to do



      SELECT top 1 g.name
      FROM group g INNER JOIN Student_group sg ON g.id = sg.Group_Id
      ORDER BY count(sg.student_id)
      GROUP BY sg.group_id





      share|improve this answer


























        2












        2








        2






        Have you tried to do



        SELECT top 1 g.name
        FROM group g INNER JOIN Student_group sg ON g.id = sg.Group_Id
        WHERE count(sg.student_id) >0
        ORDER BY count(sg.student_id)
        GROUP BY sg.group_id


        ?



        If you want also the groups with 0 students you should to do



        SELECT top 1 g.name
        FROM group g INNER JOIN Student_group sg ON g.id = sg.Group_Id
        ORDER BY count(sg.student_id)
        GROUP BY sg.group_id





        share|improve this answer














        Have you tried to do



        SELECT top 1 g.name
        FROM group g INNER JOIN Student_group sg ON g.id = sg.Group_Id
        WHERE count(sg.student_id) >0
        ORDER BY count(sg.student_id)
        GROUP BY sg.group_id


        ?



        If you want also the groups with 0 students you should to do



        SELECT top 1 g.name
        FROM group g INNER JOIN Student_group sg ON g.id = sg.Group_Id
        ORDER BY count(sg.student_id)
        GROUP BY sg.group_id






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 13:28

























        answered Nov 14 '18 at 13:22









        David Marabottini

        1707




        1707

























            2














            --If you just need the group with the least members,
            --Group By and Count will work to find the Group with the least members
            --Then use select top 1 record and order by GroupCount Ascending
            SELECT TOP 1 Group_Id, COUNT(Group_Id) AS [GroupCount]
            FROM Student_Group
            GROUP BY Group_Id
            ORDER BY [GroupCount]





            share|improve this answer

















            • 1




              How can you possibly answer this question when we have no idea what "least" is?
              – DavidG
              Nov 14 '18 at 13:16










            • I think "least" is pretty self-explanatory
              – Ryan Wilson
              Nov 14 '18 at 13:16










            • That's not what the question asked, it asked "I need to find least group of student. "
              – Ryan Wilson
              Nov 14 '18 at 13:17










            • @DavidG No where in the post does it say that.
              – Ryan Wilson
              Nov 14 '18 at 13:18










            • @DavidG I based my answer off of what the post does ask for, not some imaginary scenario Oh, look, the OP just verified my answer
              – Ryan Wilson
              Nov 14 '18 at 13:19


















            2














            --If you just need the group with the least members,
            --Group By and Count will work to find the Group with the least members
            --Then use select top 1 record and order by GroupCount Ascending
            SELECT TOP 1 Group_Id, COUNT(Group_Id) AS [GroupCount]
            FROM Student_Group
            GROUP BY Group_Id
            ORDER BY [GroupCount]





            share|improve this answer

















            • 1




              How can you possibly answer this question when we have no idea what "least" is?
              – DavidG
              Nov 14 '18 at 13:16










            • I think "least" is pretty self-explanatory
              – Ryan Wilson
              Nov 14 '18 at 13:16










            • That's not what the question asked, it asked "I need to find least group of student. "
              – Ryan Wilson
              Nov 14 '18 at 13:17










            • @DavidG No where in the post does it say that.
              – Ryan Wilson
              Nov 14 '18 at 13:18










            • @DavidG I based my answer off of what the post does ask for, not some imaginary scenario Oh, look, the OP just verified my answer
              – Ryan Wilson
              Nov 14 '18 at 13:19
















            2












            2








            2






            --If you just need the group with the least members,
            --Group By and Count will work to find the Group with the least members
            --Then use select top 1 record and order by GroupCount Ascending
            SELECT TOP 1 Group_Id, COUNT(Group_Id) AS [GroupCount]
            FROM Student_Group
            GROUP BY Group_Id
            ORDER BY [GroupCount]





            share|improve this answer












            --If you just need the group with the least members,
            --Group By and Count will work to find the Group with the least members
            --Then use select top 1 record and order by GroupCount Ascending
            SELECT TOP 1 Group_Id, COUNT(Group_Id) AS [GroupCount]
            FROM Student_Group
            GROUP BY Group_Id
            ORDER BY [GroupCount]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 13:14









            Ryan Wilson

            3,5491518




            3,5491518








            • 1




              How can you possibly answer this question when we have no idea what "least" is?
              – DavidG
              Nov 14 '18 at 13:16










            • I think "least" is pretty self-explanatory
              – Ryan Wilson
              Nov 14 '18 at 13:16










            • That's not what the question asked, it asked "I need to find least group of student. "
              – Ryan Wilson
              Nov 14 '18 at 13:17










            • @DavidG No where in the post does it say that.
              – Ryan Wilson
              Nov 14 '18 at 13:18










            • @DavidG I based my answer off of what the post does ask for, not some imaginary scenario Oh, look, the OP just verified my answer
              – Ryan Wilson
              Nov 14 '18 at 13:19
















            • 1




              How can you possibly answer this question when we have no idea what "least" is?
              – DavidG
              Nov 14 '18 at 13:16










            • I think "least" is pretty self-explanatory
              – Ryan Wilson
              Nov 14 '18 at 13:16










            • That's not what the question asked, it asked "I need to find least group of student. "
              – Ryan Wilson
              Nov 14 '18 at 13:17










            • @DavidG No where in the post does it say that.
              – Ryan Wilson
              Nov 14 '18 at 13:18










            • @DavidG I based my answer off of what the post does ask for, not some imaginary scenario Oh, look, the OP just verified my answer
              – Ryan Wilson
              Nov 14 '18 at 13:19










            1




            1




            How can you possibly answer this question when we have no idea what "least" is?
            – DavidG
            Nov 14 '18 at 13:16




            How can you possibly answer this question when we have no idea what "least" is?
            – DavidG
            Nov 14 '18 at 13:16












            I think "least" is pretty self-explanatory
            – Ryan Wilson
            Nov 14 '18 at 13:16




            I think "least" is pretty self-explanatory
            – Ryan Wilson
            Nov 14 '18 at 13:16












            That's not what the question asked, it asked "I need to find least group of student. "
            – Ryan Wilson
            Nov 14 '18 at 13:17




            That's not what the question asked, it asked "I need to find least group of student. "
            – Ryan Wilson
            Nov 14 '18 at 13:17












            @DavidG No where in the post does it say that.
            – Ryan Wilson
            Nov 14 '18 at 13:18




            @DavidG No where in the post does it say that.
            – Ryan Wilson
            Nov 14 '18 at 13:18












            @DavidG I based my answer off of what the post does ask for, not some imaginary scenario Oh, look, the OP just verified my answer
            – Ryan Wilson
            Nov 14 '18 at 13:19






            @DavidG I based my answer off of what the post does ask for, not some imaginary scenario Oh, look, the OP just verified my answer
            – Ryan Wilson
            Nov 14 '18 at 13:19




















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53301034%2fhow-to-find-least-group-of-students%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)