用sql语言查询选修课程号为1004和8001的学号。(既1004又8001)
时间: 2023-06-12 20:04:54 浏览: 295
假设学生选课信息存储在名为 `course_selection` 的表中,包含三个字段:`student_id` (学生编号)、`course_id` (课程编号)、`course_name` (课程名称)。则查询语句如下:
```
SELECT student_id FROM course_selection
WHERE course_id = '1004' AND student_id IN (
SELECT student_id FROM course_selection WHERE course_id = '8001'
)
```
解释一下查询语句:
1. `SELECT student_id`:查询结果只需要返回学生编号即可;
2. `FROM course_selection`:从 `course_selection` 表中查询;
3. `WHERE course_id = '1004'`:筛选出选修课程编号为 1004 的记录;
4. `AND student_id IN (SELECT student_id FROM course_selection WHERE course_id = '8001')`:同时筛选出选修课程编号为 8001 的记录,并且只保留学生编号在这两个子查询结果集中都出现过的记录。
相关问题
用SQL语句查询选修课程‘c01’的学生集合与选修课程‘c02’的学生集合的差集,表student记录学生的姓名和学号,表sc记录学生选修课程情况,包括选修的课程号,学生的学号(是表student的外键)。
您可以使用以下SQL语句查询选修课程‘c01’的学生集合与选修课程‘c02’的学生集合的差集:
SELECT s.s_name, s.s_id
FROM student s
LEFT JOIN sc sc1 ON s.s_id=sc1.s_id AND sc1.c_id='c01'
LEFT JOIN sc sc2 ON s.s_id=sc2.s_id AND sc2.c_id='c02'
WHERE sc1.s_id IS NOT NULL AND sc2.s_id IS NULL;
sql查询选修课程超过5门的学员学号和所属单位?
要查询选修课程超过5门的学员的学号和所属单位,可以使用如下的SQL语句:
```sql
SELECT student_id, unit
FROM (
SELECT student_id, COUNT(DISTINCT course_id) AS course_count
FROM course_enrollment
GROUP BY student_id
HAVING course_count > 5
) AS subquery
JOIN students ON subquery.student_id = students.student_id;
```
在这个语句中,假设你有一个名为 `course_enrollment` 的表来存储学员选课的情况,其中包含 `student_id` 和 `course_id` 这两个列。你还有一个名为 `students` 的表来存储学员的信息,其中包含 `student_id` 和 `unit` 这两个列。
这个查询使用了子查询来先计算每个学员选修的课程数,然后在外部查询中筛选出选修课程超过5门的学员。最后,通过将子查询结果与 `students` 表进行连接,可以获取到学员的学号和所属单位。
请注意,你需要根据你的实际数据库结构进行相应的调整。
阅读全文
相关推荐
















