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

相关推荐
程序新视界2 小时前
MySQL中,IS NULL和IS NOT NULL不会走索引?错!
数据库·mysql·dba
wdfk_prog2 小时前
闹钟定时器(Alarm Timer)初始化:构建可挂起的定时器基础框架
java·linux·数据库
许长安2 小时前
Redis(二)——Redis协议与异步方式
数据库·redis·junit
java_python源码3 小时前
python高校心理健康服务小程序(源码+文档+调试+基础修改+答疑)
数据库·sqlite
简色3 小时前
题库批量(文件)导入的全链路优化实践
java·数据库·mysql·mybatis·java-rabbitmq
点灯小铭4 小时前
基于单片机的自动存包柜设计
数据库·单片机·mongodb·毕业设计·课程设计
失散134 小时前
软件设计师——09 数据库技术基础
数据库·软考·软件设计师
养生技术人4 小时前
Oracle OCP认证考试题目详解082系列第53题
数据库·sql·oracle·database·开闭原则·ocp
银帅183350309715 小时前
2018年下半年试题四:论NoSQL数据库技术及其应用
数据库·架构·nosql
liu****5 小时前
基于websocket的多用户网页五子棋(九)
服务器·网络·数据库·c++·websocket·网络协议·个人开发