You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT
dept_name,
count( student_id ) AS student_number
FROM
department
LEFT JOIN student ONdepartment.dept_id=student.dept_idGROUP BY
dept_name
ORDER BY
dept_name DESC
解析
这题有两个要点:
哪张表连哪张表
count 处理 null 时的问题
第一点,因为现在要计算每个专业的人数,如果是 student 左连 department 就会忽略掉没有人的专业,造成最后专业缺失,所以要用 department 左连 student
题目
查询每个专业的学生人数(没有学生的专业也需要列出),将查询结果按照学生人数降序排列,如果两个或者两个以上的专业有相同的学生人数,按照专业名的字典从小到大排列
SQL
解析
这题有两个要点:
count
处理null
时的问题第一点,因为现在要计算每个专业的人数,如果是
student
左连department
就会忽略掉没有人的专业,造成最后专业缺失,所以要用department
左连student
第二点,
count(*)
计算的是总行数,不会忽略null
,但是这里有些专业没有人,计算出来应该是0
,所以应该使用count(student_id)
The text was updated successfully, but these errors were encountered: