学生成绩表 SQL 查询语句示例
学生成绩表 SQL 查询语句示例
本页面提供学生成绩表常用 SQL 查询语句示例,涵盖统计人数、查询最高分、查询个人成绩、综合查询、计算总分、平均分等功能,并包含多种条件筛选和排序方法。
数据表
-
student 表
- id: 学生 ID
- name: 学生姓名
- age: 学生年龄
- department: 学生院系
- province: 学生省份
-
score 表
- student_id: 学生 ID
- subject: 考试科目
- score: 考试成绩
查询示例
- 从 student 表中查询每个院系有多少人
SELECT department, COUNT(*) FROM student GROUP BY department;
- 从 score 表中查询每个科目的最高分
SELECT subject, MAX(score) FROM score GROUP BY subject;
- 查询李四的考试科目和考试成绩
SELECT subject, score FROM score WHERE student_id = (SELECT id FROM student WHERE name = '李四');
- 查询所有学生的信息和考试信息
SELECT * FROM student JOIN score ON student.id = score.student_id;
- 计算每个学生的总成绩
SELECT name, SUM(score) AS total_score FROM student JOIN score ON student.id = score.student_id GROUP BY name;
- 计算每个考试科目的平均成绩
SELECT subject, AVG(score) AS avg_score FROM score GROUP BY subject;
- 查询计算机成绩低于 95 的学生信息
SELECT * FROM student JOIN score ON student.id = score.student_id WHERE score < 95 AND subject = '计算机';
- 将计算机考试成绩按从高到低进行排序
SELECT * FROM score ORDER BY score DESC WHERE subject = '计算机';
- 查询姓张或姓王的学生的姓名、院系和考试科目及成绩
SELECT student.name, student.department, score.subject, score.score FROM student JOIN score ON student.id = score.student_id WHERE student.name LIKE '张%' OR student.name LIKE '王%';
- 查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
SELECT student.name, student.age, student.department, score.subject, score.score FROM student JOIN score ON student.id = score.student_id WHERE student.province = '湖南';
注意:
- 以上示例仅供参考,实际情况请根据具体需求进行调整。
- 为了提高可读性,示例代码中使用了单引号,实际操作中请根据您的数据库系统使用相应的引号。
- 建议您在使用 SQL 语句时进行验证和测试,以确保语句的正确性和安全性。
原文地址: https://www.cveoy.top/t/topic/ozFP 著作权归作者所有。请勿转载和采集!