Accepted Easy Solution Using Right Join

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

    "ORDER BY d.dept_name" is needed as well (according to the problem description), otherwise you will get a wrong answer.

  • 0

    What if the student has an undeclared major? The dept_id in the student table could be null.

Log in to reply

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