当前位置: 首页 > 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,再由小到大排序后写入一个新的文件。之后,这个新生成的文件就会取代之前已合…...

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)

题目&#xff1a;3442. 奇偶频次间的最大差值 I 思路 &#xff1a;哈希&#xff0c;时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况&#xff0c;哈希表这里用数组即可实现。 C版本&#xff1a; class Solution { public:int maxDifference(string s) {int a[26]…...

简易版抽奖活动的设计技术方案

1.前言 本技术方案旨在设计一套完整且可靠的抽奖活动逻辑,确保抽奖活动能够公平、公正、公开地进行,同时满足高并发访问、数据安全存储与高效处理等需求,为用户提供流畅的抽奖体验,助力业务顺利开展。本方案将涵盖抽奖活动的整体架构设计、核心流程逻辑、关键功能实现以及…...

Xshell远程连接Kali(默认 | 私钥)Note版

前言:xshell远程连接&#xff0c;私钥连接和常规默认连接 任务一 开启ssh服务 service ssh status //查看ssh服务状态 service ssh start //开启ssh服务 update-rc.d ssh enable //开启自启动ssh服务 任务二 修改配置文件 vi /etc/ssh/ssh_config //第一…...

MFC内存泄露

1、泄露代码示例 void X::SetApplicationBtn() {CMFCRibbonApplicationButton* pBtn GetApplicationButton();// 获取 Ribbon Bar 指针// 创建自定义按钮CCustomRibbonAppButton* pCustomButton new CCustomRibbonAppButton();pCustomButton->SetImage(IDB_BITMAP_Jdp26)…...

《用户共鸣指数(E)驱动品牌大模型种草:如何抢占大模型搜索结果情感高地》

在注意力分散、内容高度同质化的时代&#xff0c;情感连接已成为品牌破圈的关键通道。我们在服务大量品牌客户的过程中发现&#xff0c;消费者对内容的“有感”程度&#xff0c;正日益成为影响品牌传播效率与转化率的核心变量。在生成式AI驱动的内容生成与推荐环境中&#xff0…...

【算法训练营Day07】字符串part1

文章目录 反转字符串反转字符串II替换数字 反转字符串 题目链接&#xff1a;344. 反转字符串 双指针法&#xff0c;两个指针的元素直接调转即可 class Solution {public void reverseString(char[] s) {int head 0;int end s.length - 1;while(head < end) {char temp …...

JVM暂停(Stop-The-World,STW)的原因分类及对应排查方案

JVM暂停(Stop-The-World,STW)的完整原因分类及对应排查方案,结合JVM运行机制和常见故障场景整理而成: 一、GC相关暂停​​ 1. ​​安全点(Safepoint)阻塞​​ ​​现象​​:JVM暂停但无GC日志,日志显示No GCs detected。​​原因​​:JVM等待所有线程进入安全点(如…...

有限自动机到正规文法转换器v1.0

1 项目简介 这是一个功能强大的有限自动机&#xff08;Finite Automaton, FA&#xff09;到正规文法&#xff08;Regular Grammar&#xff09;转换器&#xff0c;它配备了一个直观且完整的图形用户界面&#xff0c;使用户能够轻松地进行操作和观察。该程序基于编译原理中的经典…...

A2A JS SDK 完整教程:快速入门指南

目录 什么是 A2A JS SDK?A2A JS 安装与设置A2A JS 核心概念创建你的第一个 A2A JS 代理A2A JS 服务端开发A2A JS 客户端使用A2A JS 高级特性A2A JS 最佳实践A2A JS 故障排除 什么是 A2A JS SDK? A2A JS SDK 是一个专为 JavaScript/TypeScript 开发者设计的强大库&#xff…...

08. C#入门系列【类的基本概念】:开启编程世界的奇妙冒险

C#入门系列【类的基本概念】&#xff1a;开启编程世界的奇妙冒险 嘿&#xff0c;各位编程小白探险家&#xff01;欢迎来到 C# 的奇幻大陆&#xff01;今天咱们要深入探索这片大陆上至关重要的 “建筑”—— 类&#xff01;别害怕&#xff0c;跟着我&#xff0c;保准让你轻松搞…...