SQL增查

建完库与建完表后后:

1.分别查询student表和score表的所有记录

student表:

score表:

2.查询student表的第2条到5条记录

SELECT * FROM student LIMIT 1,4;

3.从student表中查询计算机系和英语系的学生的信息

SELECT * FROM student

-> WHERE department IN ('计算机系', '英语系');

4.从student表中查询年龄小于22岁的学生信息

select * from student

-> where 2025 - birth < 22 ;

5.从student表中查询每个院系有多少人

select department, count(*) as student_count

-> from student

-> group by department;

6.从score表中查询每个科目的最高分

select c_name,max(grade) as max_grade

-> from score

-> group by c_name;

7.查询李广昌的考试科目(c name)和考试成绩(grade)

select s.c_name , s.grade

-> from student st

-> join score s on st.id = s.stu_id

-> where st.name = '李广昌';

8.用连接的方式查询所有学生的信息和考试信息

select st.*,s.c_name,s.grade

-> from student st

-> left join score s on st.id = s.stu_id;

9.计算每个学生的总成绩

select stu_id,sum(grade) as total_grade

-> from score

-> group by stu_id;

10.计算每个考试科目的平均成绩

select c_name,avg(grade) as avg_grade

-> from score

-> group by c_name;

11.查询计算机成绩低于95的学生信息

select st.*

-> from student st

-> join score s on st.id = s.stu_id

-> where s.c_name = '计算机' AND s.grade < 95

-> ;

12,将计算机考试成绩按丛高到低进行排序

select * from score

-> where c_name = '计算机'

-> order by grade desc;

13.从student表和score表中查询出学生的学号,然后合并查询结果

select id as student_id

-> from student

-> union

-> select stu_id from score;

14.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

select st.name , st.department , s.c_name , s.grade

-> from student st

-> left join score s on st.id = s.stu_id

-> where st.name like '张%' or st.name like '王%';

15.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

select st.name , 2025 - st.birth as age ,st.department , s.c_name , s.grade

-> from student st

-> left join score s on st.id = s.stu_id

-> where st.address like '%湖南省%';

相关推荐
ss27320 分钟前
为什么PostgreSQL适合AI场景?
数据库·人工智能·postgresql
瑞克-桑切斯26 分钟前
7、MySQL 慢查询排查与优化实战指南
java·服务器·数据库
z06.1 小时前
MySQL索引
数据库·mysql
考虑考虑1 小时前
Posrgresql恢复.backup文件
数据库·后端·postgresql
云祺vinchin2 小时前
中小企业备份软件选型从恢复需求到总体成本评估
数据库·数据备份·中小企业备份·容灾备份软件
Zenova EdgeOS2 小时前
Go 工业边缘错误处理实战:用 errors 构建可追溯错误链
开发语言·数据库·golang
KKKlucifer2 小时前
国内数据安全服务的产品智能化转型:大模型赋能下的技术能力重构分析
大数据·数据库·重构
字节跳动开源3 小时前
OpenViking 上线原生 3D 内容理解模型
数据库·开源
CCIE-Yasuo5 小时前
网络安全-学习数据库-Mysql-2
数据库·学习·mysql
这个DBA有点耶5 小时前
内核级AI与多模融合:AI数据库在海外场景的落地要点
数据库·人工智能·架构