数据库StudentCourse 有表SC含Sno作学号,Cno作课程号,Grade作成绩。表Student含Sno作学号,Sname作姓名,Ssex作性别,Sbirthday作出生日期,Sdept作所在系,Memo作备注。表Course含Cno作课程号,Cname作姓名,PreCourse作先行课,PreCno作选修课程号,Credit作学分,Semester作开课学期。①查询选修了【数据库原理】的计算机系的学生学号和姓名。②查询每一门课的间接先行课(印先行课的先行课)。③查询学生的学号、姓名,选修课程的名称和成绩。④查询选修了课程的学生姓名。⑤查询所有学生的信息和所选修的课程的课程号。⑥查询已被选修的课程的情况和所有课程的名字。⑦列出学生所有可能的选修情况。⑧查找计算机系的学生选修课程数大于2的学生的姓名、平均成绩和选课门数,并按平均成绩降序排列:
时间: 2023-11-16 21:08:22 浏览: 91
①查询选修了【数据库原理】的计算机系的学生学号和姓名。
```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;
```
阅读全文