查询每门课程平均成绩大于80分的信息 数据库原理
时间: 2024-10-17 18:14:50 浏览: 30
查询每门课程平均成绩大于80分的信息,在数据库原理中通常涉及到SQL(结构化查询语言)查询。假设我们有一个包含课程信息和学生成绩的表,比如`Courses` 和 `Grades`,它们之间可能通过课程ID关联。一个基本的SQL查询可能是这样的:
```sql
SELECT Courses.CourseName, AVG(Grades.Score) as AverageScore
FROM Courses
JOIN Grades ON Courses.CourseID = Grades.CourseID
GROUP BY Courses.CourseName
HAVING AVG(Grades.Score) > 80;
```
这个查询做了以下几件事:
1. `JOIN` 表示将`Courses`和`Grades`表基于`CourseID`字段连接起来。
2. `AVG(Grades.Score)` 计算每个课程的成绩平均值。
3. `GROUP BY Courses.CourseName` 按照课程名对结果进行分组。
4. `HAVING AVG(Grades.Score) > 80` 过滤出平均成绩大于80的课程。
相关问题
MySQL以数据库user_db及数据表student、teacher、class、course、sc为基础(11)查询选修“数据库原理”的学生的最高成绩、最低成绩、平均成绩; (12)查询平均成绩在60分以上的课程及其平均成绩; (13)查询平均成绩大于“高莉”的平均成绩的学生学号、姓名及平均成绩; (14)查询每门课程中成绩最低的学生的学号、姓名、性别、院系; (15)查询只被一名学生选修的课程的课程号、课程名。
11.
```
SELECT MAX(score) AS max_score, MIN(score) AS min_score, AVG(score) AS avg_score
FROM sc
WHERE course_name = '数据库原理';
```
12.
```
SELECT course_id, course_name, AVG(score) AS avg_score
FROM sc
GROUP BY course_id, course_name
HAVING AVG(score) > 60;
```
13.
```
SELECT s.stu_id, s.stu_name, AVG(score) AS avg_score
FROM student s, sc c
WHERE s.stu_id = c.stu_id
GROUP BY s.stu_id, s.stu_name
HAVING AVG(score) > (SELECT AVG(score) FROM sc WHERE stu_name = '高莉');
```
14.
```
SELECT s.stu_id, s.stu_name, s.gender, s.department, c.course_name, MIN(score) AS min_score
FROM student s, sc c
WHERE s.stu_id = c.stu_id
AND score = (SELECT MIN(score) FROM sc WHERE course_name = c.course_name)
GROUP BY c.course_name, s.stu_id, s.stu_name, s.gender, s.department;
```
15.
```
SELECT course_id, course_name
FROM sc
GROUP BY course_id, course_name
HAVING COUNT(DISTINCT stu_id) = 1;
```
多表连接查询 (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;
```
阅读全文