SQL查询SC数据库表选修课程数大于等于2 的学生的学号、平均成绩和选课门数,并按 平均成绩降序排列的代码。
时间: 2024-05-30 12:14:26 浏览: 186
SELECT student_id, AVG(grade) as avg_grade, COUNT(course_id) as num_courses
FROM SC
GROUP BY student_id
HAVING COUNT(course_id) >= 2
ORDER BY avg_grade DESC;
相关问题
表SC 列名有Sno作学号 Cno作选课课程号 Grade作成绩 查询选修课程数大于等于2的学生的学号、平均成绩和选课门 数,并按平 均成绩降序排列SQL查询
以下是你所需的 SQL 查询:
```
SELECT Sno, AVG(Grade) AS AvgGrade, COUNT(DISTINCT Cno) AS CourseCount
FROM SC
GROUP BY Sno
HAVING COUNT(DISTINCT Cno) >= 2
ORDER BY AvgGrade DESC;
```
这个查询首先从 SC 表中选出学号(Sno)、成绩(Grade)和课程号(Cno)列。然后,它按照学号进行分组,并计算平均成绩和选课门数。最后,筛选出选修课程数大于等于2的学生,并按照平均成绩降序排列输出结果。
数据库StudentCourse 有表SC含Sno作学号,Cno作课程号,Grade作成绩。表Student含Sno作学号,Sname作姓名,Ssex作性别,Sbirthday作出生日期,Sdept作所在系,Memo作备注。表Course含Cno作课程号,Cname作姓名,PreCourse作先行课,PreCno作选修课程号,Credit作学分,Semester作开课学期。①查询选修了【数据库原理】的计算机系的学生学号和姓名。②查询每一门课的间接先行课(印先行课的先行课)。③查询学生的学号、姓名,选修课程的名称和成绩。④查询选修了课程的学生姓名。⑤查询所有学生的信息和所选修的课程的课程号。⑥查询已被选修的课程的情况和所有课程的名字。⑦列出学生所有可能的选修情况。⑧查找计算机系的学生选修课程数大于2的学生的姓名、平均成绩和选课门数,并按平均成绩降序排列:
①查询选修了【数据库原理】的计算机系的学生学号和姓名。
```sql
SELECT Student.Sno, Student.Sname
FROM Student, SC, Course
WHERE Student.Sno = SC.Sno AND SC.Cno = Course.Cno AND Course.Cname = '数据库原理' AND Student.Sdept = '计算机系';
```
②查询每一门课的间接先行课(印先行课的先行课)。
```sql
WITH RECURSIVE pre_course(cno, pre_cno) AS (
SELECT Cno, PreCno FROM Course WHERE PreCno IS NOT NULL
UNION ALL
SELECT pre_course.cno, Course.PreCno FROM pre_course JOIN Course ON pre_course.pre_cno = Course.Cno
)
SELECT cno, group_concat(DISTINCT pre_cno) as indirect_pre_courses
FROM pre_course
GROUP BY cno;
```
③查询学生的学号、姓名,选修课程的名称和成绩。
```sql
SELECT Student.Sno, Student.Sname, Course.Cname, SC.Grade
FROM Student, SC, Course
WHERE Student.Sno = SC.Sno AND SC.Cno = Course.Cno;
```
④查询选修了课程的学生姓名。
```sql
SELECT DISTINCT Student.Sname
FROM Student, SC, Course
WHERE Student.Sno = SC.Sno AND SC.Cno = Course.Cno AND Course.Cname = '数据库原理';
```
⑤查询所有学生的信息和所选修的课程的课程号。
```sql
SELECT Student.*, SC.Cno
FROM Student LEFT JOIN SC ON Student.Sno = SC.Sno;
```
⑥查询已被选修的课程的情况和所有课程的名字。
```sql
SELECT Course.*, SC.Sno, SC.Grade
FROM Course LEFT JOIN SC ON Course.Cno = SC.Cno;
```
⑦列出学生所有可能的选修情况。
```sql
SELECT Student.Sno, Student.Sname, Course.Cno, Course.Cname
FROM Student CROSS JOIN Course;
```
⑧查找计算机系的学生选修课程数大于2的学生的姓名、平均成绩和选课门数,并按平均成绩降序排列。
```sql
SELECT Student.Sname, AVG(SC.Grade) AS avg_grade, COUNT(*) as course_count
FROM Student JOIN SC ON Student.Sno = SC.Sno JOIN Course ON SC.Cno = Course.Cno
WHERE Student.Sdept = '计算机系'
GROUP BY Student.Sno
HAVING COUNT(*) > 2
ORDER BY AVG(SC.Grade) DESC;
```
阅读全文