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

相关推荐
这个DBA有点耶1 小时前
AI写的SQL跑崩了生产库,这锅谁背?
数据库·人工智能·程序员
镜舟科技2 小时前
Databricks 再提 LTAP,AI 时代的数据底座为何重回大一统叙事?
数据库·架构·agent
Databend2 小时前
从湖仓升级为 Agent 时代的数据控制面,Snowflake 和 Databricks 有哪些布局
大数据·数据库·agent
ClouGence6 小时前
SQL Server CDC 能放到 Always On 备库读吗?一文讲透原理与实践
数据库·sql server
先吃饱再说1 天前
存储的进化:从 MySQL 到浏览器缓存,数据到底住在哪?
数据库
Nturmoils1 天前
字段太多看不全,ksql 的展开模式和输出控制怎么用
数据库·后端
Databend1 天前
Agent 轨迹分析与归因的数据工程实践
大数据·数据库·agent
这个DBA有点耶1 天前
SQL改写进阶:标量子查询的“隐形代价”与消除实战
数据库·mysql·架构
smallyoung1 天前
数据库乐观锁深度解析:MySQL、PostgreSQL 实战 + Spring Boot 集成指南
数据库·mysql·postgresql
parade岁月1 天前
MySQL JOIN解析:朴实无华但食之有味
数据库·后端