当前位置: 首页 > news >正文

MySQL45道练习题

作业需要数据表SQL语句已给

986efecee4084e6f8b44c13030f9f398.png

 1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数

select * from Student RIGHT JOIN (select t1.SId, class1, class2 from(select SId, score as class1 from sc where sc.CId = '01')as t1, (select SId, score as class2 from sc where sc.CId = '02')as t2where t1.SId = t2.SId AND t1.class1 > t2.class2
)r on Student.SId = r.SId;

 1.1 查询同时存在" 01 "课程和" 02 "课程的情况

select * from (select * from sc where sc.CId = '01') as t1, (select * from sc where sc.CId = '02') as t2
where t1.SId = t2.SId;

1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )

select * from 
(select * from sc where sc.CId = '01') as t1
left join 
(select * from sc where sc.CId = '02') as t2
on t1.SId = t2.SId;

1.3 查询不存在" 01 "课程但存在" 02 "课程的情况

select * from sc
where sc.SId not in (select SId from sc where sc.CId = '01'
) and sc.CId= '02';

2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

select student.SId,sname,ss from student,(select SId, AVG(score) as ss from sc  group by SId having AVG(score)> 60)r where student.sid = r.sid;

3.查询在 SC 表存在成绩的学生信息

select distinct student.*
from student,sc
where student.SId=sc.SId

4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

select s.sid, s.sname,r.coursenumber,r.scoresum
from ((select student.sid,student.sname from student)s left join (select sc.sid, sum(sc.score) as scoresum, count(sc.cid) as coursenumber from sc group by sc.sid)r on s.sid = r.sid);

 

4.1 查有成绩的学生信息

select * from student where exists (select sc.sid from sc where student.sid = sc.sid);

5.查询「李」姓老师的数量

select count(*) from teacher where tname like '李%';

6.查询学过「张三」老师授课的同学的信息

select student.* from student,teacher,course,sc
where student.sid = sc.sid and course.cid=sc.cid and course.tid = teacher.tid and tname = '张三';

7.查询没有学全所有课程的同学的信息

select * from student
where student.sid not in (select sc.sid from scgroup by sc.sidhaving count(sc.cid)= (select count(cid) from course));

8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信

select * from student 
where student.sid in (select sc.sid from sc where sc.cid in(select sc.cid from sc where sc.sid = '01'));

9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息

select * from student where sid in(
select a.sid from (select sid,group_concat(cid order by cid) as courses 
from sc group by sid) as a
inner join (select sid,group_concat(cid order by cid) as courses 
from sc group by sid having sid='01') as b
on a.sid != 1 and a.courses = b.courses);

10.查询没学过"张三"老师讲授的任一门课程的学生姓名

select * from student
where student.sid not in(select sc.sid from sc,course,teacher wheresc.cid = course.cidand course.tid = teacher.tidand teacher.tname= "张三");

11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select student.sid, student.Sname,b.avg
from student right join
(select sid, AVG(score) as avg from scwhere sid in (select sid from sc where score<60 group by sid having count(score)>1)group by sid) b on student.sid=b.sid;

12.检索" 01 "课程分数小于 60,按分数降序排列的学生信息

select student.*, sc.score from student, sc
where student.sid = sc.sid
and sc.score < 60
and cid = "01"
order by sc.score desc;

13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select *  from sc 
left join (select sid,avg(score) as avscore from sc group by sid)r 
on sc.sid = r.sid
order by avscore desc;

14.查询各科成绩最高分、最低分和平均分:

以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率;及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90;要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select 
sc.CId ,
max(sc.score)as 最高分,
min(sc.score)as 最低分,
AVG(sc.score)as 平均分,
count(*)as 选修人数,
sum(case when sc.score>=60 then 1 else 0 end )/count(*)as 及格率,
sum(case when sc.score>=70 and sc.score<80 then 1 else 0 end )/count(*)as 中等率,
sum(case when sc.score>=80 and sc.score<90 then 1 else 0 end )/count(*)as 优良率,
sum(case when sc.score>=90 then 1 else 0 end )/count(*)as 优秀率 
from sc
GROUP BY sc.CId
ORDER BY count(*)DESC, sc.CId ASC

15.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺

SELECT *,row_number() over (PARTITION BY cid ORDER BY score DESC) AS '排名'FROM sc;

15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次

select *,
case when @sco=score then @rank else @rank:=@rank+1 end as rn,
@sco:=score
from sc,(select @rank:=0,@sco:=NULL) t
order by score desc;

16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺

select a.*,
@rank:=if(@sco=scos,'',@rank+1) as rn,
@sco:=scos
from
(select sid,sum(score) as scos from sc
group by sid order by scos desc) a,
(select @rank:=0,@sco:=NULL) b;

16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

select a.*,
@rank:=if(@sco=scos,@rank,@rank+1) as rn,
@sco:=scos
from
(select sid,sum(score) as scos from sc
group by sid order by scos desc) a,
(select @rank:=0,@sco:=NULL) b;

17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比

select course.cname, course.cid,
sum(case when sc.score<=100 and sc.score>85 then 1 else 0 end) as "[100-85]",
sum(case when sc.score<=85 and sc.score>70 then 1 else 0 end) as "[85-70]",
sum(case when sc.score<=70 and sc.score>60 then 1 else 0 end) as "[70-60]",
sum(case when sc.score<=60 and sc.score>0 then 1 else 0 end) as "[60-0]"
from sc left join course on sc.cid = course.cid group by sc.cid;

18.查询各科成绩前三名的记录

select * from sc
where (
select count(*) from sc as a 
where sc.cid = a.cid and sc.score<a.score 
)< 3 order by cid asc, sc.score desc;

19.查询每门课程被选修的学生数

select cid, count(sid) from sc  group by cid;

20.查询出只选修两门课程的学生学号和姓名

select student.SId,student.Sname
from sc,student
where student.SId=sc.SId  
group by sc.SId
having count(*)=2;

21.查询男生、女生人数

select ssex, count(*) from student group by ssex;

22.查询名字中含有「风」字的学生信息

select * from student  where student.Sname like '%风%';

23.查询同名同性学生名单,并统计同名人数

select sname, count(*) from student group by sname having count(*)>1;

24.查询 1990 年出生的学生名单

select * from student where YEAR(student.Sage)=1990;

25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select sc.cid, course.cname, AVG(SC.SCORE) as average from sc, course
where sc.cid = course.cid group by sc.cid order by average desc,cid asc;

26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩

select student.sid, student.sname, AVG(sc.score) as aver from student, sc
where student.sid = sc.sid group by sc.sid having aver > 85;

27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数

select student.sname, sc.score from student, sc, course
where student.sid = sc.sid and course.cid = sc.cid and course.cname = "数学" and sc.score < 60;

28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)

select student.sname, cid, score from student left join sc on student.sid = sc.sid;

29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数

select student.sname, course.cname,sc.score from student,course,sc
where sc.score>70 and student.sid = sc.sid and sc.cid = course.cid;

30.查询不及格的课程

select distinct sc.CId from sc where sc.score <60;

31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名

select student.sid,student.sname from student,sc
where cid="01" and score>=80 and student.sid = sc.sid;

32.求每门课程的学生人数

select sc.cid,count(*) as 学生人数 from sc group by sc.cid;

33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

select student.*, sc.score, sc.cid from student, teacher, course,sc 
where teacher.tid = course.tid
and sc.sid = student.sid
and sc.cid = course.cid
and teacher.tname = "张三"
order by score desc
limit 1;

34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

select student.*, sc.score, sc.cid from student, teacher, course,sc 
where teacher.tid = course.tid
and sc.sid = student.sid
and sc.cid = course.cid
and teacher.tname = "张三"
and sc.score = (select Max(sc.score) from sc,student, teacher, coursewhere teacher.tid = course.tidand sc.sid = student.sidand sc.cid = course.cidand teacher.tname = "张三"
);

35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

select  a.cid, a.sid,  a.score from sc as a
inner join 
sc as b
on a.sid = b.sid
and a.cid != b.cid
and a.score = b.score
group by cid, sid;

36.查询每门功成绩最好的前两名

select a.sid,a.cid,a.score from sc as a left join sc as b 
on a.cid = b.cid and a.score<b.score
group by a.cid, a.sid having count(b.cid)<2 order by a.cid;

37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。

select sc.cid, count(sid) as cc from sc group by cid having cc >5;

38.检索至少选修两门课程的学生学号

select sid, count(cid) as cc from sc group by sid having cc>=2;

39.查询选修了全部课程的学生信息

select student.* from sc ,student where sc.SId=student.SId group by sc.SId having count(*) = (select distinct count(*) from course )

40.查询各学生的年龄,只按年份来算

select *, (year(now()) - year(sage)) as age from student;

41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

select *, timestampdiff(YEAR,sage,NOW()) as age from student;

42.查询本周过生日的学生

select * from student where WEEKOFYEAR(student.Sage)=WEEKOFYEAR(CURDATE());

43.查询下周过生日的学生

select * from student where WEEKOFYEAR(student.Sage)=WEEKOFYEAR(CURDATE())+1;

44.查询本月过生日的学生

select * from student where MONTH(student.Sage)=MONTH(CURDATE());

45.查询下月过生日的学生

select * from student where MONTH(student.Sage)=MONTH(CURDATE())+1;

 

 

相关文章:

MySQL45道练习题

作业需要数据表SQL语句已给 1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数 select * from Student RIGHT JOIN (select t1.SId, class1, class2 from(select SId, score as class1 from sc where sc.CId 01)as t1, (select SId, score as …...

HTML5和CSS3的新特性

HTML5的新特性主要是针对于以前的不足&#xff0c;增加了一些新的标签、新的表单和新的表单属性等 1&#xff0c;HTML5新增的语义化标签 <header> 头部标签 <nav> 导航标签 <article> …...

【MySQL】表列数和行大小限制详解

目录 限制维度 列数量限制 表的最大行大小 单个列的存储要求 存储引擎的附加限制 功能键部分 行容量限制 MySQL表的内部实现 InnoDB表的最大行大小 超出InnoDB最大行大小的处理 不同存储格式的影响 限制示例 行大小限制示例 InnoDB下 MyISAM下 InnoDB变长情况示…...

算法基础学习|双指针算法

双指针算法 代码模板 for (int i 0, j 0; i < n; i ){while (j < i && check(i, j)) j ;// 具体问题的逻辑 } 常见问题分类&#xff1a;(1) 对于一个序列&#xff0c;用两个指针维护一段区间(2) 对于两个序列&#xff0c;维护某种次序&#xff0c;比如归并…...

4.远程登录服务

目录 1. 简介 1.1. 概念 1.2. 功能: 1.3. 分类 1.3.1. 文字接口: 1.3.2. 图形接口&#xff1a; 1.4. 文字接口连接服务器: 2. 连接加密技术简介 2.1. 密钥解析&#xff1a; 3. SSH工作过程&#xff1a; 3.1. 版本协商阶段 3.2. 密钥和算法协商阶段 3.3. 认证阶段(两…...

代码随想录算法训练营第二十九天| 491.递增子序列、46.全排列、47.全排列 II

491.递增子序列 题目链接&#xff1a;力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台 解题思路&#xff1a;同层相同元素要跳过 java&#xff1a; class Solution {List<List<Integer>> resultnew ArrayList<>();List<Integ…...

基于若依的ruoyi-nbcio流程管理系统一种简单的动态表单模拟测试实现(五)

更多ruoyi-nbcio功能请看演示系统 gitee源代码地址 前后端代码&#xff1a; https://gitee.com/nbacheng/ruoyi-nbcio 演示地址&#xff1a;RuoYi-Nbcio后台管理系统 更多nbcio-boot功能请看演示系统 gitee源代码地址 后端代码&#xff1a; https://gitee.com/nbacheng/n…...

多场景建模:阿里多场景多任务元学习方法M2M

multi-scenario multi-task meta learning approach (M2M) 背景 广告领域大部分是针对用户建模的&#xff0c;像点击率预估&#xff0c;很少有针对广告主需求建模&#xff08;广告消耗预估、活跃率/流失率预估、广告曝光量预估&#xff09;&#xff0c;广告的类型较多&#x…...

仿真机器人-深度学习CV和激光雷达感知(项目2)day03【机器人简介与ROS基础】

文章目录 前言机器人简介机器人应用与前景机器人形态机器人的构成 ROS基础ROS的作用和特点ROS的运行机制ROS常用命令 前言 &#x1f4ab;你好&#xff0c;我是辰chen&#xff0c;本文旨在准备考研复试或就业 &#x1f4ab;本文内容是我为复试准备的第二个项目 &#x1f4ab;欢迎…...

【多商户开源-BSD- Fecmall 电商平台】

关于Fecmall Fecmall 关于&#xff0c;Fecmall介绍 Fecbbc开源BSD多商户系统&#xff0c;真正开源&#xff0c;商用免费授权的多商户系统 Fecmall系统简介&#xff1a; 全称为Fancy ECommerce Shop&#xff0c; 着重于电商架构的研发优化&#xff0c;全新定义商城的架构体系&…...

2023春秋杯冬季赛 --- Crypto wp

文章目录 前言Cryptonot_wiener 前言 比赛没打&#xff0c;赛后随便做一下题目 Crypto not_wiener task.py: from Crypto.Util.number import * from gmpy2 import * import random, os from hashlib import sha1 from random import randrange flagb x bytes_to_long(f…...

ImageMagick使用手册

ImageMagick使用手册 文章目录 ImageMagick使用手册ImageMagick是什么指令总结1.查看版本2.官网验证指令解读3.svg转png4.png转ico5.将指定文件夹中的所有PNG图像转换为ICO图标 本教程持续更新&#xff0c;我学到多少就更新多少。关于ffmpeg如何安装可以看上一篇博客win系统环境…...

嵌入式培训机构四个月实训课程笔记(完整版)-C++和QT编程第五天-Qt编程技巧若干解答(物联技术666)

链接&#xff1a;https://pan.baidu.com/s/1-u7GvgM0TLuiy9z7LYQ80Q?pwd1688 提取码&#xff1a;1688 QT在Windows下制作图表 QT是跨平台的程序设计库&#xff0c;在windows下的程序往往想要有一个好看的图标&#xff0c;方法如下&#xff1a; 1.准备个ICO图标。例如&…...

【蓝桥杯选拔赛真题59】python小写字母 第十五届青少年组蓝桥杯python 选拔赛比赛真题解析

目录 python小写字母 一、题目要求 1、编程实现 2、输入输出...

代码随想录算法训练营Day37|738.单调递增的数字、贪心算法总结

目录 738.单调递增的数字 方法一&#xff1a;暴力解法 方法二&#xff1a;贪心解法 贪心算法总结 738.单调递增的数字 题目链接 文章链接 方法一&#xff1a;暴力解法 class Solution { private:// 各位递增判断函数bool checkNum(int num) {int max 10;while (num) {int …...

笔记-影响力-对比,互惠,赌徒原理

笔记 From 《影响力》 目录 C1 影响力的武器 C2:互惠 C3 承诺和一致 C4:社会认同 C5:喜好 C6:权威 C7:稀缺 Ankie的笔记&#xff1a;对比&#xff0c;互惠&#xff0c;赌徒原理 C1 影响力的武器 人类在认知方面有个原理&#xff0c;叫“对比原理”。 假设有人走进一…...

PIL、cv2、numpy,和pytorch(torch)之间的转换

在图像处理和深度学习中&#xff0c;经常需要在PIL&#xff08;Python Imaging Library&#xff09;、OpenCV&#xff08;cv2&#xff09;、NumPy和PyTorch之间进行图像数据的转换。下面是这些库之间常见的转换方法。 1. PIL和numpy之间的转换 import numpy as np from PIL i…...

Java面试题50道

文章目录 1.谈谈你对Spring的理解2.Spring的常用注解有哪些3.Spring中的bean线程安全吗4.Spring中的设计模式有哪些5.Spring事务传播行为有几种6.Spring是怎么解决循环依赖的7.SpringBoot自动配置原理8.SpringBoot配置文件类型以及加载顺序9.SpringCloud的常用组件有哪些10.说一…...

电脑怎么剪辑视频?这些软件不可错过

电脑怎么剪辑视频&#xff1f;随着视频时代的到来&#xff0c;越来越多的人开始涉足视频制作。从Vlog到短片&#xff0c;再到专业级的影视作品&#xff0c;视频已经成为了我们生活中不可或缺的一部分。那么&#xff0c;如何才能快速掌握视频剪辑技巧&#xff0c;制作出令人惊叹…...

HBase学习七:Compaction

1、简介 Compaction是从一个Region的一个Store中选择部分HFile文件进行合并。其目的为了减少 HFile 的个数跟清理掉过期和删除的数据。 合并原理是,先从这些待合并的数据文件中依次读出KeyValue,再由小到大排序后写入一个新的文件。之后,这个新生成的文件就会取代之前已合…...

<6>-MySQL表的增删查改

目录 一&#xff0c;create&#xff08;创建表&#xff09; 二&#xff0c;retrieve&#xff08;查询表&#xff09; 1&#xff0c;select列 2&#xff0c;where条件 三&#xff0c;update&#xff08;更新表&#xff09; 四&#xff0c;delete&#xff08;删除表&#xf…...

聊聊 Pulsar:Producer 源码解析

一、前言 Apache Pulsar 是一个企业级的开源分布式消息传递平台&#xff0c;以其高性能、可扩展性和存储计算分离架构在消息队列和流处理领域独树一帜。在 Pulsar 的核心架构中&#xff0c;Producer&#xff08;生产者&#xff09; 是连接客户端应用与消息队列的第一步。生产者…...

智能在线客服平台:数字化时代企业连接用户的 AI 中枢

随着互联网技术的飞速发展&#xff0c;消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁&#xff0c;不仅优化了客户体验&#xff0c;还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用&#xff0c;并…...

Redis数据倾斜问题解决

Redis 数据倾斜问题解析与解决方案 什么是 Redis 数据倾斜 Redis 数据倾斜指的是在 Redis 集群中&#xff0c;部分节点存储的数据量或访问量远高于其他节点&#xff0c;导致这些节点负载过高&#xff0c;影响整体性能。 数据倾斜的主要表现 部分节点内存使用率远高于其他节…...

虚拟电厂发展三大趋势:市场化、技术主导、车网互联

市场化&#xff1a;从政策驱动到多元盈利 政策全面赋能 2025年4月&#xff0c;国家发改委、能源局发布《关于加快推进虚拟电厂发展的指导意见》&#xff0c;首次明确虚拟电厂为“独立市场主体”&#xff0c;提出硬性目标&#xff1a;2027年全国调节能力≥2000万千瓦&#xff0…...

WPF八大法则:告别模态窗口卡顿

⚙️ 核心问题&#xff1a;阻塞式模态窗口的缺陷 原始代码中ShowDialog()会阻塞UI线程&#xff0c;导致后续逻辑无法执行&#xff1a; var result modalWindow.ShowDialog(); // 线程阻塞 ProcessResult(result); // 必须等待窗口关闭根本问题&#xff1a…...

Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement

Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement 1. LAB环境2. L2公告策略2.1 部署Death Star2.2 访问服务2.3 部署L2公告策略2.4 服务宣告 3. 可视化 ARP 流量3.1 部署新服务3.2 准备可视化3.3 再次请求 4. 自动IPAM4.1 IPAM Pool4.2 …...

SQL Server 触发器调用存储过程实现发送 HTTP 请求

文章目录 需求分析解决第 1 步:前置条件,启用 OLE 自动化方式 1:使用 SQL 实现启用 OLE 自动化方式 2:Sql Server 2005启动OLE自动化方式 3:Sql Server 2008启动OLE自动化第 2 步:创建存储过程第 3 步:创建触发器扩展 - 如何调试?第 1 步:登录 SQL Server 2008第 2 步…...

水泥厂自动化升级利器:Devicenet转Modbus rtu协议转换网关

在水泥厂的生产流程中&#xff0c;工业自动化网关起着至关重要的作用&#xff0c;尤其是JH-DVN-RTU疆鸿智能Devicenet转Modbus rtu协议转换网关&#xff0c;为水泥厂实现高效生产与精准控制提供了有力支持。 水泥厂设备众多&#xff0c;其中不少设备采用Devicenet协议。Devicen…...

加密通信 + 行为分析:运营商行业安全防御体系重构

在数字经济蓬勃发展的时代&#xff0c;运营商作为信息通信网络的核心枢纽&#xff0c;承载着海量用户数据与关键业务传输&#xff0c;其安全防御体系的可靠性直接关乎国家安全、社会稳定与企业发展。随着网络攻击手段的不断升级&#xff0c;传统安全防护体系逐渐暴露出局限性&a…...