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 '%湖南省%';

相关推荐
异世界贤狼转生码农1 小时前
MongoDB Windows 系统实战手册:从配置到数据处理入门
数据库·mongodb
QuZhengRong1 小时前
【数据库】Navicat 导入 Excel 数据乱码问题的解决方法
android·数据库·excel
码农阿豪1 小时前
Windows从零到一安装KingbaseES数据库及使用ksql工具连接全指南
数据库·windows
时序数据说7 小时前
时序数据库市场前景分析
大数据·数据库·物联网·开源·时序数据库
听雪楼主.10 小时前
Oracle Undo Tablespace 使用率暴涨案例分析
数据库·oracle·架构
我科绝伦(Huanhuan Zhou)10 小时前
KINGBASE集群日常维护管理命令总结
数据库·database
妖灵翎幺11 小时前
Java应届生求职八股(2)---Mysql篇
数据库·mysql
HMBBLOVEPDX11 小时前
MySQL的事务日志:
数据库·mysql
YA33312 小时前
java基础(九)sql基础及索引
java·开发语言·sql
weixin_4196583113 小时前
MySQL数据库备份与恢复
数据库·mysql