考试报名
考试报名
考试内容
考试大纲
在线客服
返回顶部

备考刷题,请到

CDA认证小程序

设数据库中有以下三张表:sc(sno,cno,grade),course(cno,cname,hours),student(sno,sname,ssex,sage,sdept)。试用SQL语言写出以下查询。 查询选课门数超过2门的学生的姓名、平均成绩和选课门数
A. SELECT student.sname 姓名,AVG(sc.grade) 平均成绩,COUNT(sc.sno) 选课门数FROM sc JOIN student ON sc.sno = student.sno GROUP BY student.sname HAVING COUNT(distinct sc.sno) >2;
B. SELECT student.sname 姓名,AVG(sc.grade) 平均成绩,COUNT(sc.cno) 选课门数FROM sc JOIN student ON sc.sno = student.sno GROUP BY student.sname HAVING COUNT(distinct sc.sno) >2;
C. SELECT student.sname 姓名,AVG(sc.grade) 平均成绩,COUNT(distinct sc.sno) 选课门数FROM sc JOIN student ON sc.sno = student.sno GROUP BY student.sname HAVING COUNT(sc.sno) >2;
D. SELECT student.sname 姓名,AVG(sc.grade) 平均成绩,COUNT(distinct sc.cno) 选课门数FROM sc JOIN student ON sc.sno = student.sno GROUP BY student.sname HAVING COUNT(distinct sc.cno) >2;
上一题
下一题
收藏
点赞
评论
题目解析
题目评论(0)

统计选课门数应该使用COUNT(distinct sc.cno)

正确答案是:D: SELECT student.sname 姓名, AVG(sc.grade) 平均成绩, COUNT(distinct sc.cno) 选课门数 FROM sc JOIN student ON sc.sno = student.sno GROUP BY student.sname HAVING COUNT(distinct sc.cno) > 2;

专业分析如下:

1. **查询选课门数超过2门的学生**:
- 需要统计每个学生所选的课程数目,并且这个数目要大于2。
- 使用`COUNT(distinct sc.cno)`来统计每个学生选的不同课程数目。

2. **返回学生的姓名、平均成绩和选课门数**:
- 学生姓名:`student.sname`
- 平均成绩:`AVG(sc.grade)`
- 选课门数:`COUNT(distinct sc.cno)`

3. **表连接**:
- 使用`JOIN`将`sc`表和`student`表连接起来,通过`sc.sno = student.sno`进行连接。

4. **分组和筛选**:
- 使用`GROUP BY student.sname`对结果进行分组。
- 使用`HAVING COUNT(distinct sc.cno) > 2`来筛选选课门数超过2门的学生。

具体分析每个选项:

- **选项A**:`COUNT(sc.sno)`统计的是每个学生在`sc`表中的记录数,而不是不同课程数目。
- **选项B**:`COUNT(sc.cno)`统计的是每个学生在`sc`表中的记录数,而不是不同课程数目。
- **选项C**:`COUNT(distinct sc.sno)`统计的是不同学生数目,不符合题意。
- **选项D**:`COUNT(distinct sc.cno)`统计的是每个学生选的不同课程数目,符合题意。

因此,选项D是正确的。