在学生课程数据库中,使用连接查询实现: ■1)查询每个学生的情况及其选修课程 2)求学生学号、姓名、选修课程名及成绩 ■3)求选修C1课程且成绩为90分以上的学生学号、姓名及成绩 ”4)查询每一门课程的间接先行课(即先行课的先行课) 3.嵌套查询 口在学生课程数据库中,使用嵌套查询实现: 口1)求选修了计算机基础的学生学号和姓名 口2)求选修C3课程且成绩高于程宏的学生学号和成绩 3) 求其他系中 比计算机系某一学生年龄小的学生4)求其他系中比计算机系所有学生年龄都小的学生口5)求选修了C2课程的学生姓名 口6)求没有选修C2课程的学生姓名 7 )查询选修了全部课程的学生姓名 8)求至少选修了学号为“s2”的学生所选修的全部课程的学生学号和姓名
时间: 2023-11-10 20:10:31 浏览: 360
使用连接查询实现:
1)查询每个学生的情况及其选修课程
```
SELECT s.id, s.name, c.course_name
FROM student s
LEFT JOIN score sc ON s.id = sc.student_id
LEFT JOIN course c ON sc.course_id = c.id;
```
2)求学生学号、姓名、选修课程名及成绩
```
SELECT s.id, s.name, c.course_name, sc.score
FROM student s
LEFT JOIN score sc ON s.id = sc.student_id
LEFT JOIN course c ON sc.course_id = c.id;
```
3)求选修C1课程且成绩为90分以上的学生学号、姓名及成绩
```
SELECT s.id, s.name, sc.score
FROM student s
LEFT JOIN score sc ON s.id = sc.student_id
LEFT JOIN course c ON sc.course_id = c.id
WHERE c.course_name = 'C1' AND sc.score >= 90;
```
4)查询每一门课程的间接先行课(即先行课的先行课)
```
SELECT c1.course_name, c2.course_name
FROM course c1
JOIN prerequisite p ON c1.id = p.course_id
JOIN course c2 ON p.prerequisite_id = c2.id;
```
使用嵌套查询实现:
1)求选修了计算机基础的学生学号和姓名
```
SELECT id, name
FROM student
WHERE id IN (SELECT student_id FROM score WHERE course_id = (SELECT id FROM course WHERE course_name = '计算机基础'));
```
2)求选修C3课程且成绩高于程宏的学生学号和成绩
```
SELECT s.id, sc.score
FROM student s
JOIN score sc ON s.id = sc.student_id
WHERE sc.course_id = (SELECT id FROM course WHERE course_name = 'C3')
AND sc.score > (SELECT score FROM score WHERE student_id = (SELECT id FROM student WHERE name = '程宏') AND course_id = (SELECT id FROM course WHERE course_name = 'C3'));
```
3) 求其他系中比计算机系某一学生年龄小的学生
```
SELECT id, name
FROM student
WHERE department != '计算机系' AND age < (SELECT age FROM student WHERE name = '某一学生' AND department = '计算机系');
```
4)求其他系中比计算机系所有学生年龄都小的学生
```
SELECT id, name
FROM student
WHERE department != '计算机系' AND age < ALL (SELECT age FROM student WHERE department = '计算机系');
```
5)求选修了C2课程的学生姓名
```
SELECT name
FROM student
WHERE id IN (SELECT student_id FROM score WHERE course_id = (SELECT id FROM course WHERE course_name = 'C2'));
```
6)求没有选修C2课程的学生姓名
```
SELECT name
FROM student
WHERE id NOT IN (SELECT student_id FROM score WHERE course_id = (SELECT id FROM course WHERE course_name = 'C2'));
```
7)查询选修了全部课程的学生姓名
```
SELECT s.name
FROM student s
JOIN (SELECT student_id FROM score GROUP BY student_id HAVING COUNT(course_id) = (SELECT COUNT(*) FROM course)) sc ON s.id = sc.student_id;
```
8)求至少选修了学号为“s2”的学生所选修的全部课程的学生学号和姓名
```
SELECT s.id, s.name
FROM student s
JOIN (SELECT course_id FROM score WHERE student_id = 's2') c ON s.id = (SELECT student_id FROM score WHERE course_id = c.course_id)
GROUP BY s.id, s.name
HAVING COUNT(DISTINCT c.course_id) = (SELECT COUNT(*) FROM score WHERE student_id = 's2');
```
阅读全文