查询选修课程一样且成绩相同的学生进本情况(使用自连接查询)
时间: 2023-12-02 10:05:18 浏览: 87
好的,针对您的问题,以下是 SQL 查询语句:
```
SELECT DISTINCT a.student_id, b.student_id, a.course_id, a.score
FROM score a
INNER JOIN score b ON a.course_id = b.course_id AND a.score = b.score AND a.student_id < b.student_id
```
这是一个自连接查询,我们将成绩表 score 分别命名为 a 和 b,然后通过内连接查询满足条件的数据,即选修课程一样且成绩相同的学生信息。使用 DISTINCT 去重,避免重复数据。
希望能够帮到您!
相关问题
数据库查询选修了课程且成绩不及格的学生信息
好的,假设您的数据库中有以下三张表:
1. 学生表(students),包括以下字段:
- 学生编号(id)
- 学生姓名(name)
- 年龄(age)
- 性别(gender)
- 所在班级(class)
2. 课程表(courses),包括以下字段:
- 课程编号(id)
- 课程名称(name)
- 授课教师(teacher)
- 学分(credit)
3. 成绩表(scores),包括以下字段:
- 学生编号(student_id)
- 课程编号(course_id)
- 成绩(score)
那么,查询选修了课程且成绩不及格的学生信息的 SQL 语句如下:
```
SELECT students.id, students.name, students.age, students.gender, students.class
FROM students
INNER JOIN scores ON students.id = scores.student_id
INNER JOIN courses ON scores.course_id = courses.id
WHERE scores.score < 60
```
这个 SQL 语句中,使用了 INNER JOIN 连接了三张表,其中 scores 表和 courses 表连接的条件是 course_id = courses.id,students 表和 scores 表连接的条件是 students.id = scores.student_id。最后,使用 WHERE 子句筛选出成绩小于60分的学生信息。
查询选修oracle课程并且成绩高于oracle课程平均成绩的学生
好的,你可以使用以下 SQL 语句来查询选修 Oracle 课程且成绩高于该课程平均成绩的学生:
```
SELECT s.student_id, s.student_name, c.course_name, sc.score
FROM student s
JOIN score sc ON s.student_id = sc.student_id
JOIN course c ON sc.course_id = c.course_id
WHERE c.course_name = 'Oracle' AND sc.score > (
SELECT AVG(sc2.score)
FROM score sc2
JOIN course c2 ON sc2.course_id = c2.course_id
WHERE c2.course_name = 'Oracle'
)
```
上述 SQL 语句中,使用 `SELECT` 查询语句查询符合条件的学生信息。首先,使用 `JOIN` 连接三张表(student、score、course),通过学生 ID、课程 ID 进行关联。接着,使用 `WHERE` 子句筛选选修 Oracle 课程的成绩,并且成绩高于该课程平均成绩的学生。在 `WHERE` 子句中,使用子查询获取 Oracle 课程的平均成绩,并与当前行的成绩进行比较。最后,使用 `SELECT` 子句返回学生 ID、学生姓名、课程名称、成绩等信息。
相关推荐
![application/x-dosexec](https://img-home.csdnimg.cn/images/20210720083343.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://csdnimg.cn/download_wenku/file_type_ask_c1.png)