Why group by d.dept_id, but not s.dept_id?


  • 0
    A

    Can someone please explain why grouping by d.dept_id works, but s.dept_id failed with the following test case? Thanks for your time.

    {"headers":{"student":["student_id","student_name","gender","dept_id"],"department":["dept_id","dept_name"]},"rows":{"student":[[1,"Will","F",7],[2,"Jane","M",5],[3,"Alex","F",4],[4,"Bill","M",4],[8,"Bezalel","M",3],[9,"Parto","F",9]],"department":[[1,"Architecture"],[2,"Art"],[3,"Biotechnology"],[4,"East Asian Studies"],[5,"Engineering"],[7,"Law"],[9,"Politics"]]}}
    

    #passed
    SELECT dept_name, COUNT(student_id) AS student_number
    FROM student s
    RIGHT OUTER JOIN department d
    ON s.dept_id = d.dept_id
    GROUP BY d.dept_id
    ORDER BY student_number DESC, dept_name

    #failed: it shows ["Architecture", 0], but not ["Art", 0]]
    SELECT dept_name, COUNT(student_id) AS student_number
    FROM student s
    RIGHT OUTER JOIN department d
    ON s.dept_id = d.dept_id
    GROUP BY s.dept_id
    ORDER BY student_number DESC, dept_name


  • 0
    D

    @Alpher I have the same doubt.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.