在student、sc和course表中查询和学号为103的学生所选修课程至少有一门相同的学生的学号,课程号和成绩。
时间: 2024-10-21 11:06:37 浏览: 25
这是一个涉及到数据库查询的问题,假设我们有三个相关的表:
1. `student` 表:包含学生的相关信息,如 `id`(学号)等字段。
2. `sc` 表:记录学生选课信息,包括 `sid`(学生ID,关联到 `student.id`)、`cid`(课程ID,关联到 `course.id`)和 `score`(成绩)字段。
3. `course` 表:存储课程数据,如 `id`(课程号)。
要查询和学号为103的学生(`sid = 103`)选修课程至少有一门相同的其他学生的学号、课程号和成绩,你可以构造这样的SQL查询:
```sql
SELECT s1.id AS student_id, c.course_id, c.score
FROM student s1
JOIN sc s1_sc ON s1.id = s1_sc.sid
JOIN course c ON s1_sc.cid = c.id
WHERE s1.id = 103
AND EXISTS (
SELECT 1
FROM sc s2_sc
JOIN course c ON s2_sc.cid = c.id
WHERE s2_sc.sid != s1.id AND s1_sc.cid = s2_sc.cid
)
```
这个查询首先找到学号为103的学生的所有选课(`s1_sc`),然后找出是否有其他学生的课程与其有相同的课程ID(`c`)。如果有,则返回那些学生的学号(`s1.id`)、对应的课程号(`c.course_id`)以及成绩(`c.score`)。
相关问题
数据库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;
```
(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;
```
阅读全文