查询学生的学号姓名选修的课程编号,课程名称和成绩,没有选课的同学的选课信息显示为空
时间: 2024-11-27 16:21:26 浏览: 47
查询学生的信息通常涉及到数据库操作,例如在一个包含学生、课程和选课记录的关联数据模型中,你可以通过SQL查询来获取所需的数据。假设我们有三个表:`students`(存储学生信息),`courses`(存储课程信息)以及`course_enrollments`(存储选课关系),查询可以类似这样编写:
```sql
SELECT students.student_id AS 学号,
students.name AS 姓名,
courses.course_id AS 课程编号,
courses.course_name AS 课程名称,
course_enrollments.score AS 成绩
FROM students
LEFT JOIN course_enrollments ON students.student_id = course_enrollments.student_id
JOIN courses ON course_enrollments.course_id = courses.course_id
ORDER BY students.name;
```
这个查询使用了`LEFT JOIN`来包括所有学生,即使他们没有选课,选课信息列(如score)将显示为NULL。
相关问题
多表连接查询 (1) 查询选修了【数据库原理】的计算机系的学生学号和姓名。 (2) 查询每一门课的间接先行课(即先行课的先行课),显示课程编号和该门课程的间接先行课编号。 (3) 查询学生的学号、姓名、选修课程的名称和成绩。 (4) 查询选修了课程的学生姓名。 (5) 查询所有学生的信息和所选修的课程, 要求显示学生的学号、姓名、课程号及课程名。没有选课的同学对应的选课信息为空。 (6) 查询所有课程的课程编号、课程名称及选课人数,没有被选的课程选课人数显示为0。 (7) 列出所有学生所有可能的选修情况,要求显示学生的学号、姓名、课程号及课程名。 (8) 查找计算机系的学生选修课程数大于2 的学生的姓名、 平均成绩和选课门数,并按平均成绩降序排列。
1.
```
SELECT s.id, s.name
FROM student s
JOIN course_selection cs ON s.id = cs.student_id
JOIN course c ON cs.course_id = c.id
WHERE s.department = '计算机系'
AND c.name = '数据库原理';
```
2.
```
WITH RECURSIVE prerequisites AS (
SELECT course_id, prerequisite_id
FROM prerequisite
UNION
SELECT p.course_id, pr.prerequisite_id
FROM prerequisites pr
JOIN prerequisite p ON pr.course_id = p.prerequisite_id
)
SELECT c.id, pr.prerequisite_id
FROM course c
LEFT JOIN prerequisites pr ON c.id = pr.course_id;
```
3.
```
SELECT s.id, s.name, c.name, cs.grade
FROM student s
JOIN course_selection cs ON s.id = cs.student_id
JOIN course c ON cs.course_id = c.id;
```
4.
```
SELECT s.name
FROM student s
JOIN course_selection cs ON s.id = cs.student_id;
```
5.
```
SELECT s.id, s.name, c.id, c.name
FROM student s
LEFT JOIN course_selection cs ON s.id = cs.student_id
LEFT JOIN course c ON cs.course_id = c.id;
```
6.
```
SELECT c.id, c.name, COUNT(cs.student_id) AS num_students
FROM course c
LEFT JOIN course_selection cs ON c.id = cs.course_id
GROUP BY c.id, c.name;
```
7.
```
SELECT s.id, s.name, c.id, c.name
FROM student s
CROSS JOIN course c;
```
8.
```
WITH course_counts AS (
SELECT s.id, s.name, COUNT(cs.course_id) AS num_courses
FROM student s
JOIN course_selection cs ON s.id = cs.student_id
WHERE s.department = '计算机系'
GROUP BY s.id, s.name
HAVING COUNT(cs.course_id) > 2
),
average_grades AS (
SELECT s.id, AVG(cs.grade) AS avg_grade
FROM student s
JOIN course_selection cs ON s.id = cs.student_id
GROUP BY s.id
)
SELECT cc.name, ag.avg_grade, cc.num_courses
FROM course_counts cc
JOIN average_grades ag ON cc.id = ag.id
ORDER BY ag.avg_grade DESC;
```
(1)查询选修课程号为C01,并且成绩及格的学生学号、姓名和系别名称。 (2)查询计算机学院选修C03课程的学生姓名、课程名称以及这门课程的成绩。(3)在Reports表中查询至少选修课了程号为C01和C02的学生学号和姓名。(4)查询既没有选修课程号C01,也没有选修课程号C02的学生的学号和姓名。(5) 查询每个同学基本信息及其选修课程的情况。(6)查询每个学生及其选修课程的情况(若某一个学生没有选课,只输出学生的基本情况信息,其选课信息为空值)。 (7)查询选修了课程名为“GIS开发”的学生学号和姓名。(8) 查询没有选修C02课程的学生学号和姓名。 (9)查询所有课程成绩均大于90分的学生学号和姓名。(10)查询选修了4门及以上课程的学生的学号、姓名和选修课程数。(11) 查询选修全部课程的学生姓名,相当于查询这样的学生,没有一门课程是他不选的。 (12) 查询平均成绩大于等于黄河同学的平均成绩的学生的学号、姓名和平均成绩。(13)查询每门课程获得最高分的同学的学号、姓名、课程名称和成绩,并按照最高成绩从高到低进行排序。
很好,您的问题是关于 SQL 语句的。以下是您提出的问题的 SQL 查询语句:
(1)查询选修课程号为C01,并且成绩及格的学生学号、姓名和系别名称。
```
SELECT StudentID, StudentName, DepartmentName
FROM Student, Department, SC
WHERE Student.StudentID = SC.StudentID
AND SC.CourseID = 'C01'
AND SC.Score >= 60
AND Student.DepartmentID = Department.DepartmentID;
```
(2)查询计算机学院选修C03课程的学生姓名、课程名称以及这门课程的成绩。
```
SELECT StudentName, CourseName, Score
FROM Student, Course, SC, Department
WHERE Student.StudentID = SC.StudentID
AND Course.CourseID = SC.CourseID
AND Department.DepartmentID = Student.DepartmentID
AND Department.DepartmentName = '计算机学院'
AND Course.CourseID = 'C03';
```
(3)在Reports表中查询至少选修课了程号为C01和C02的学生学号和姓名。
```
SELECT StudentID, StudentName
FROM Reports
WHERE CourseID IN ('C01','C02')
GROUP BY StudentID, StudentName
HAVING COUNT(DISTINCT CourseID) >= 2;
```
(4)查询既没有选修课程号C01,也没有选修课程号C02的学生的学号和姓名。
```
SELECT StudentID, StudentName
FROM Student
WHERE StudentID NOT IN (
SELECT StudentID
FROM SC
WHERE CourseID IN ('C01','C02')
);
```
(5)查询每个同学基本信息及其选修课程的情况。
```
SELECT Student.StudentID, Student.StudentName, Student.Gender, Student.Birthday, Student.Address, Department.DepartmentName, SC.CourseID, Course.CourseName, SC.Score
FROM Student
LEFT JOIN SC ON Student.StudentID = SC.StudentID
LEFT JOIN Course ON SC.CourseID = Course.CourseID
LEFT JOIN Department ON Student.DepartmentID = Department.DepartmentID
ORDER BY Student.StudentID, SC.CourseID;
```
(6)查询每个学生及其选修课程的情况(若某一个学生没有选课,只输出学生的基本情况信息,其选课信息为空值)。
```
SELECT Student.StudentID, Student.StudentName, Student.Gender, Student.Birthday, Student.Address, Department.DepartmentName, SC.CourseID, Course.CourseName, SC.Score
FROM Student
LEFT JOIN SC ON Student.StudentID = SC.StudentID
LEFT JOIN Course ON SC.CourseID = Course.CourseID
LEFT JOIN Department ON Student.DepartmentID = Department.DepartmentID
ORDER BY Student.StudentID, SC.CourseID;
```
(7)查询选修了课程名为“GIS开发”的学生学号和姓名。
```
SELECT Student.StudentID, Student.StudentName
FROM Student, SC, Course
WHERE Student.StudentID = SC.StudentID
AND SC.CourseID = Course.CourseID
AND Course.CourseName = 'GIS开发';
```
(8)查询没有选修C02课程的学生学号和姓名。
```
SELECT Student.StudentID, Student.StudentName
FROM Student
WHERE Student.StudentID NOT IN (
SELECT StudentID
FROM SC
WHERE CourseID = 'C02'
);
```
(9)查询所有课程成绩均大于90分的学生学号和姓名。
```
SELECT StudentID, StudentName
FROM SC, Student
WHERE SC.StudentID = Student.StudentID
AND Score >= 90
GROUP BY StudentID, StudentName
HAVING COUNT(DISTINCT CourseID) = (
SELECT COUNT(DISTINCT CourseID)
FROM Course
);
```
(10)查询选修了4门及以上课程的学生的学号、姓名和选修课程数。
```
SELECT StudentID, StudentName, COUNT(*) AS CourseCount
FROM SC, Student
WHERE SC.StudentID = Student.StudentID
GROUP BY StudentID, StudentName
HAVING COUNT(*) >= 4;
```
(11)查询选修全部课程的学生姓名,相当于查询这样的学生,没有一门课程是他不选的。
```
SELECT Student.StudentName
FROM Student
WHERE NOT EXISTS (
SELECT CourseID
FROM Course
WHERE NOT EXISTS (
SELECT SC.StudentID
FROM SC
WHERE SC.StudentID = Student.StudentID
AND SC.CourseID = Course.CourseID
)
);
```
(12)查询平均成绩大于等于黄河同学的平均成绩的学生的学号、姓名和平均成绩。
```
SELECT AVG(SC.Score) AS AvgScore, Student.StudentID, Student.StudentName
FROM SC, Student
WHERE SC.StudentID = Student.StudentID
GROUP BY Student.StudentID, Student.StudentName
HAVING AVG(SC.Score) >= (
SELECT AVG(SC.Score)
FROM SC, Student
WHERE SC.StudentID = Student.StudentID
AND Student.StudentName = '黄河'
);
```
(13)查询每门课程获得最高分的同学的学号、姓名、课程名称和成绩,并按照最高成绩从高到低进行排序。
```
SELECT SC.StudentID, Student.StudentName, Course.CourseName, MAX(SC.Score) AS MaxScore
FROM SC, Student, Course
WHERE SC.StudentID = Student.StudentID
AND SC.CourseID = Course.CourseID
GROUP BY SC.CourseID
ORDER BY MaxScore DESC;
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)