SQL语句测试
班级表 学生表 分数表 课程表 老师表
前提:
1. 一个班级具有多个学生
2. 分数应该体现学生对应的课程
3. 一个老师可以带多个班级
sql语句执行顺序:
1、from
2、where
3、group by ··· having
4、聚合函数
5、select查询语句
6、order by排序语句
···
# 1 查询所有课程(course)以及对应老师(teacher)的名称
select cname, tname from course left join teacher on course.teacher_id = teacher.tid;
# left join: 左侧有的全显示,左侧没有的不显示,链接关系为外键字段
# 2 查询学生表(student)中男女生各有多少人
select gender, count(sid) from student group by gender;
# group by: 以某个字段进行分组,count对分组内容进行统计
# 3 查询物理(course)成绩(score)等于100的学生(student)姓名
select sname from student where sid in (select student_id from score where course_id = (select cid from course where cname = '物理') and num = 100)
# 子查询: 1. 查找物理课程 2. 查看分数等于100的1 3. 查询获取该分数的学生姓名
# 4 查询平均成绩(score)大于80分的同学(student)的姓名跟平均成绩
select sname, st.avg_num from student inner join (select student_id, avg(num) avg_num from score group by student_id having avg(num) > 80) as st on student.sid = score.student_id;
# 查询平均成绩大于80分的同学id inner join 与之关联查看学生姓名
# 5 查询所有学生(student)的学号,姓名,选课数(course),总成绩(score)
select sid 学号, sname 姓名, count_course_id 选课数, sum_num 总成绩 from student inner join (select student_id, count(course_id) count_course_id, sum(num) sum_num from score group by student_id) as st on student.sid = st.student_id;
# 根据学生分组查询选课数,总成绩,inner join与之关联查看学生学号,姓名
# 6 查询李姓老师(teacher)的个数
select count(tid) from teacher where tname like '李%';
# 模糊匹配过滤查找
# 7 查询没有报李平老师(teacher)课程(course)的学生(student)姓名
select sname from student where sid not in (select distinct student_id from score where course_id in (select cid from course where teacher_id = (select tid from teacher where tname = '李平老师')))
# 查看学生姓名 not in(报选 李平老师的课程的学生)
# 8 查询物理课程(course)比生物课程分数(score)高的学生学号跟姓名
select t1.student_id from (select student_id, num from score inner join course on score.course_id = course.cid where course.cname = '物理') as t1 inner join (select student_id, num from score inner join course on score.course_id = course.cid where course.cname = '生物') as t2 on t1.student_id = t2.student_id where t1.num > t2.num;
# 分别查询物理成绩跟生物成绩。inner join 比较
# 9 查询没有同时(score)选修物理课程(course)跟体育课程的学生(student)姓名
select sname from student where sid in (select student_id from score inner join course on course.cid = socre.course_id and course.cname in ('物理', '体育') group by student_id having count(course_id) = 1);
# 查询符合规则的学生姓名(选修了一门课程的学生)
# 10 查询挂科超过两门(course)的学生姓名(student)和班级(class)
select t2.sname, class.caption from (select sname, class_id from student inner join (select student_id from score where num < 60 group by student_id having count(course_id) >= 2) as t1 on student.sid = t1.student_id ) as t2 inner join class on class.cid = t2.class_id;
#
# 11 查询选修了所有课程(course)的学生姓名(student)
select sname from student inner join (select student_id from score group by student_id having count(course_id) = (select count(cid) from course )) as t1 on t1.student_id = student.sid;
#
# 12 查询李平老师(student)教的课程(course)的所有成绩(score)记录
# 13 查询全部学生(student)都选修了的课程号跟课程名(course)
# 14 查询每门课程(course)被选修的次数(score)
# 15 查询只选修了一门课程(course)的学生(student)的学号跟姓名
# 16 查询所有学生(student)考出的成绩(score)并从高到低排序(去重)
# 17 查询平均成绩(score)大于85的学生(student)姓名跟平均成绩
# 18 查询生物(course)成绩(score)不及格的学生(student)姓名和对应的生物分数
# 19 查询所有选修了李平(teacher)老师课程(course)的学生(student)中,这些课程(李平老师的课程)平均成绩最高的学生姓名
# 20 查询每门课程成绩(score)最好的前两名学生(student)姓名
# 21 查询不同课程但成绩(score)相同的学号,课程号,成绩
# 22 查询没学过“叶平”老师(teacher)课程的学生(student)姓名以及选修的课程(course)名称;
# 23 查询所有选修了学号为1的同学选修过的一门或者多门课程的同学学号和姓名
# 24 任课最多的老师中学生单科成绩最高的学生姓名
https://tool.lu/
https://cli.im/
https://0x3.com/
https://www.uupoop.com/
https://www.hipdf.cn/
https://showmore.com/zh/
http://weibodang.cn/
sqlite3 版本升级:https://www.cnblogs.com/yoyo1216/p/14927862.html
本文来自博客园,作者:一石数字欠我15w!!!,转载请注明原文链接:https://www.cnblogs.com/52-qq/p/15883648.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· c# 半导体/led行业 晶圆片WaferMap实现 map图实现入门篇
· 易语言 —— 开山篇