查询所有成绩为空的学生的学号、姓名、选修课程号和课程名。
时间: 2024-11-12 16:37:00 浏览: 12
查询所有成绩为空(NULL)的学生的学号、姓名、选修课程号和课程名,需要假设我们有两个表,一个是`Students`,包含字段`StudentID`, `Name`, 和`ClassID`;另一个是`Courses`,包含字段`CourseID`和`CourseName`,以及一个关联表`Enrollments`用于存储学生选课信息,包含字段`StudentID`和`CourseID`。
**SQL查询实现:**
```sql
SELECT s.StudentID, s.Name, ec.CourseID, c.CourseName
FROM Students s
LEFT JOIN Enrollments e ON s.StudentID = e.StudentID
LEFT JOIN Courses c ON e.CourseID = c.CourseID
WHERE e.CourseID IS NULL;
```
这个查询首先通过左连接`Enrollments`和`Courses`表,使得每个学生的所有选课情况都显示出来。然后在`WHERE`子句中,筛选出那些`CourseID`在关联表中不存在(即没有成绩)的学生。
相关问题
(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;
```
11查询选修了“C02”的学生的姓名和所在系。 12 查询成绩在80分以上的学生的姓名,课程号和成绩并按成绩的降序排列。 13 查询哪些课程没有人选修,要求列出课程号和课程名。14 查询缺少了成绩的学生的学号和课程号。 15查询数学系学生的学号和姓名。 16 查询选修了课程的学生学号 17 查询选修了数学课的学生学号和成绩,并要求对查询结果按成绩降序排列,如果成绩相同则按学号升序排列。 18查询选修数学课其且成绩在80-90之间的学生学号和成绩,并将成绩乘以系数0.8输出。 19 查询数学系或计算机系姓刘的学生的信息。
11. 查询选修了“C02”的学生的姓名和所在系。
```sql
SELECT s.name, d.name
FROM student s, department d, course c, sc
WHERE s.sdept = d.did AND s.sno = sc.sno AND c.cno = sc.cno AND c.cname = 'C02';
```
12. 查询成绩在80分以上的学生的姓名,课程号和成绩并按成绩的降序排列。
```sql
SELECT s.name, sc.cno, sc.grade
FROM student s, sc
WHERE s.sno = sc.sno AND sc.grade >= 80
ORDER BY sc.grade DESC;
```
13. 查询哪些课程没有人选修,要求列出课程号和课程名。
```sql
SELECT c.cno, c.cname
FROM course c
WHERE NOT EXISTS (SELECT * FROM sc WHERE c.cno = sc.cno);
```
14. 查询缺少了成绩的学生的学号和课程号。
```sql
SELECT sc.sno, sc.cno
FROM sc
WHERE sc.grade IS NULL;
```
15. 查询数学系学生的学号和姓名。
```sql
SELECT s.sno, s.name
FROM student s, department d
WHERE s.sdept = d.did AND d.name = '数学系';
```
16. 查询选修了课程的学生学号。
```sql
SELECT DISTINCT sc.sno
FROM sc;
```
17. 查询选修了数学课的学生学号和成绩,并要求对查询结果按成绩降序排列,如果成绩相同则按学号升序排列。
```sql
SELECT sc.sno, sc.grade
FROM student s, course c, sc
WHERE s.sno = sc.sno AND c.cno = sc.cno AND c.cname LIKE '%数学%'
ORDER BY sc.grade DESC, sc.sno ASC;
```
18. 查询选修数学课其且成绩在80-90之间的学生学号和成绩,并将成绩乘以系数0.8输出。
```sql
SELECT sc.sno, sc.grade * 0.8 AS grade
FROM student s, course c, sc
WHERE s.sno = sc.sno AND c.cno = sc.cno AND c.cname LIKE '%数学%' AND sc.grade BETWEEN 80 AND 90;
```
19. 查询数学系或计算机系姓刘的学生的信息。
```sql
SELECT s.*
FROM student s, department d
WHERE s.sdept = d.did AND (d.name = '数学系' OR d.name = '计算机系') AND s.name LIKE '刘%';
```
阅读全文