mysql-面试50题-2

 一、查询数据

学生表 Student

create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));

insert into Student values('01' , '赵雷' , '1990-01-01' , '男');

insert into Student values('02' , '钱电' , '1990-12-21' , '男');

insert into Student values('03' , '孙风' , '1990-12-20' , '男');

insert into Student values('04' , '李云' , '1990-12-06' , '男');

insert into Student values('05' , '周梅' , '1991-12-01' , '女');

insert into Student values('06' , '吴兰' , '1992-01-01' , '女');

insert into Student values('07' , '郑竹' , '1989-01-01' , '女');

insert into Student values('09' , '张三' , '2017-12-20' , '女');

insert into Student values('10' , '李四' , '2017-12-25' , '女');

insert into Student values('11' , '李四' , '2012-06-06' , '女');

insert into Student values('12' , '赵六' , '2013-06-13' , '女');

insert into Student values('13' , '孙七' , '2014-06-01' , '女');

科目表 Course

create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));

insert into Course values('01' , '语文' , '02');

insert into Course values('02' , '数学' , '01');

insert into Course values('03' , '英语' , '03');

教师表 Teacher

create table Teacher(TId varchar(10),Tname varchar(10));

insert into Teacher values('01' , '张三');

insert into Teacher values('02' , '李四');

insert into Teacher values('03' , '王五');

成绩表 SC

create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));

insert into SC values('01' , '01' , 80);

insert into SC values('01' , '02' , 90);

insert into SC values('01' , '03' , 99);

insert into SC values('02' , '01' , 70);

insert into SC values('02' , '02' , 60);

insert into SC values('02' , '03' , 80);

insert into SC values('03' , '01' , 80);

insert into SC values('03' , '02' , 80);

insert into SC values('03' , '03' , 80);

insert into SC values('04' , '01' , 50);

insert into SC values('04' , '02' , 30);

insert into SC values('04' , '03' , 20);

insert into SC values('05' , '01' , 76);

insert into SC values('05' , '02' , 87);

insert into SC values('06' , '01' , 31);

insert into SC values('06' , '03' , 34);

insert into SC values('07' , '02' , 89);

insert into SC values('07' , '03' , 98);

二、问题

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

从SC表中选取score小于60的,并group by sid,having count 大于1,然后再在sc表中,查询平均成绩,最后与student表联合查询。

mysql> select student.SId, student.Sname,b.avg
    -> from student RIGHT JOIN
    -> (select sid, AVG(score) as avg from sc
    ->     where 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;

+------+--------+----------+
| SId  | Sname  | avg      |
+------+--------+----------+
| 04   | 李云   | 33.33333 |
| 06   | 吴兰   | 32.50000 |
+------+--------+----------+
2 rows in set (0.06 sec)

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

这道题简单,就不多说了。

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

+------+--------+---------------------+------+-------+
| SId  | Sname  | Sage                | Ssex | score |
+------+--------+---------------------+------+-------+
| 04   | 李云   | 1990-12-06 00:00:00 | 男   |  50.0 |
| 06   | 吴兰   | 1992-01-01 00:00:00 | 女   |  31.0 |
+------+--------+---------------------+------+-------+
2 rows in set (0.00 sec)

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

我的评价是,逻辑上没多大难度,写上也没有多少难度

mysql> 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;

+------+------+-------+------+----------+
| SId  | CId  | score | sid  | avscore  |
+------+------+-------+------+----------+
| 07   | 02   |  89.0 | 07   | 93.50000 |
| 07   | 03   |  98.0 | 07   | 93.50000 |
| 01   | 03   |  99.0 | 01   | 89.66667 |
| 01   | 02   |  90.0 | 01   | 89.66667 |
| 01   | 01   |  80.0 | 01   | 89.66667 |
| 05   | 02   |  87.0 | 05   | 81.50000 |
| 05   | 01   |  76.0 | 05   | 81.50000 |
| 03   | 01   |  80.0 | 03   | 80.00000 |
| 03   | 03   |  80.0 | 03   | 80.00000 |
| 03   | 02   |  80.0 | 03   | 80.00000 |
| 02   | 02   |  60.0 | 02   | 70.00000 |
| 02   | 01   |  70.0 | 02   | 70.00000 |
| 02   | 03   |  80.0 | 02   | 70.00000 |
| 04   | 01   |  50.0 | 04   | 33.33333 |
| 04   | 03   |  20.0 | 04   | 33.33333 |
| 04   | 02   |  30.0 | 04   | 33.33333 |
| 06   | 01   |  31.0 | 06   | 32.50000 |
| 06   | 03   |  34.0 | 06   | 32.50000 |
+------+------+-------+------+----------+
18 rows in set (0.00 sec)

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

以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率,及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

这道题看起来要求很多,但从下面代码可以看出来,其实,这就是一个单表查询,其难点在于限制条件的书写。

mysql> 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;

+------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+
| CId  | 最高分    | 最低分    | 平均分    | 选修人数     | 及格率    | 中等率    | 优良率    | 优秀率    |
+------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+
| 01   |      80.0 |      31.0 |  64.50000 |            6 |    0.6667 |    0.3333 |    0.3333 |    0.0000 |
| 02   |      90.0 |      30.0 |  72.66667 |            6 |    0.8333 |    0.0000 |    0.5000 |    0.1667 |
| 03   |      99.0 |      20.0 |  68.50000 |            6 |    0.6667 |    0.0000 |    0.3333 |    0.3333 |
+------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+
3 rows in set (0.00 sec)

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

mysql>  select a.cid, a.sid, a.score, count(b.score)+1 as rank
    -> from sc as a
    -> left join sc as b
    -> on a.score<b.score and a.cid = b.cid
    -> group by a.cid, a.sid,a.score
    -> order by a.cid, rank asc;

+------+------+-------+------+
| cid  | sid  | score | rank |
+------+------+-------+------+
| 01   | 01   |  80.0 |    1 |
| 01   | 03   |  80.0 |    1 |
| 01   | 05   |  76.0 |    3 |
| 01   | 02   |  70.0 |    4 |
| 01   | 04   |  50.0 |    5 |
| 01   | 06   |  31.0 |    6 |
| 02   | 01   |  90.0 |    1 |
| 02   | 07   |  89.0 |    2 |
| 02   | 05   |  87.0 |    3 |
| 02   | 03   |  80.0 |    4 |
| 02   | 02   |  60.0 |    5 |
| 02   | 04   |  30.0 |    6 |
| 03   | 01   |  99.0 |    1 |
| 03   | 07   |  98.0 |    2 |
| 03   | 03   |  80.0 |    3 |
| 03   | 02   |  80.0 |    3 |
| 03   | 06   |  34.0 |    5 |
| 03   | 04   |  20.0 |    6 |
+------+------+-------+------+
18 rows in set (0.00 sec)

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

mysql> select q.sid, total, @crank := @crank +1 as rank from(
    -> select sc.sid, sum(sc.score) as total from sc
    -> group by sc.sid
    -> order by total desc)q;

+------+-------+------+
| sid  | total | rank |
+------+-------+------+
| 01   | 269.0 |    1 |
| 03   | 240.0 |    2 |
| 02   | 210.0 |    3 |
| 07   | 187.0 |    4 |
| 05   | 163.0 |    5 |
| 04   | 100.0 |    6 |
| 06   |  65.0 |    7 |
+------+-------+------+
7 rows in set (0.00 sec)

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

mysql> select sc.cid,
    -> max(sc.score) 最高分,
    -> min(sc.score) 最低分,
    -> avg(sc.score) 平均分,
    -> count(*) 参选人数,
    -> sum(case when sc.score>=60 then 1 else 0 end)/count(*) "[60-0]",
    -> sum(case when sc.score>=60 and sc.score<70 then 1 else 0 end)/count(*) "[70-60]",
    -> sum(case when sc.score>=70 and sc.score<85 then 1 else 0 end)/count(*) "[85-70]",
    -> sum(case when sc.score>=85 then 1 else 0 end)/count(*) "[100-85]"
    -> from sc
    -> group by sc.cid
    -> order by sc.cid asc;

+------+-----------+-----------+-----------+--------------+--------+---------+---------+----------+
| cid  | 最高分    | 最低分    | 平均分    | 参选人数     | [60-0] | [70-60] | [85-70] | [100-85] |
+------+-----------+-----------+-----------+--------------+--------+---------+---------+----------+
| 01   |      80.0 |      31.0 |  64.50000 |            6 | 0.6667 |  0.0000 |  0.6667 |   0.0000 |
| 02   |      90.0 |      30.0 |  72.66667 |            6 | 0.8333 |  0.1667 |  0.1667 |   0.5000 |
| 03   |      99.0 |      20.0 |  68.50000 |            6 | 0.6667 |  0.0000 |  0.3333 |   0.3333 |
+------+-----------+-----------+-----------+--------------+--------+---------+---------+----------+
3 rows in set (0.00 sec)

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

mysql> 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;

+------+------+-------+
| SId  | CId  | score |
+------+------+-------+
| 01   | 01   |  80.0 |
| 03   | 01   |  80.0 |
| 05   | 01   |  76.0 |
| 01   | 02   |  90.0 |
| 07   | 02   |  89.0 |
| 05   | 02   |  87.0 |
| 01   | 03   |  99.0 |
| 07   | 03   |  98.0 |
| 02   | 03   |  80.0 |
| 03   | 03   |  80.0 |
+------+------+-------+
10 rows in set (0.00 sec)

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

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

+------+------------+
| cid  | count(sid) |
+------+------------+
| 01   |          6 |
| 02   |          6 |
| 03   |          6 |
+------+------------+
3 rows in set (0.00 sec)

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

mysql> select student.sid, student.sname from student
    -> where student.sid in
    -> (select sc.sid from sc
    -> group by sc.sid
    -> having count(sc.cid)=2
    -> );

+------+--------+
| sid  | sname  |
+------+--------+
| 05   | 周梅   |
| 06   | 吴兰   |
| 07   | 郑竹   |
+------+--------+
3 rows in set (0.03 sec)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/107332.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

如何理解my_map.yaml中origin的含义

当然可以。首先,我们先了解一下2D地图的基本构成。2D地图实际上是一个网格系统,其中每个单元格(或像素)代表现实世界中的一个区域。当我们谈论origin时,我们实际上是在描述这个网格如何在真实的3D空间中放置。 让我们通过一个简单的示意图来解释: 假设上面的矩形表示一个…

【可变形注意力(1)】Multi-scale Deformable Attention Transformers 多尺度变形注意力

文章目录 前言论文 《Deformable DETR: Deformable Transformers for End-to-End Object Detection》的多尺度变形注意力的解读DEFORMABLE TRANSFORMERS FOR END-TO-END OBJECT DETECTION **2.** Deformable Attention ModuleDeformable Attention Module 3. Multi-Scale Defor…

【开源】基于SpringBoot的森林火灾预警系统的设计和实现

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 数据中心模块2.2 系统基础模块2.3 烟雾传感器模块2.4 温度传感器模块2.5 历史记录模块2.6 园区数据模块 三、系统设计3.1 用例设计3.1.1 森林园区基础系统用例设计3.1.2 森林预警数据用例设计 3.2 数据库设计3.2.1 烟雾…

C# | Chaikin算法 —— 计算折线对应的平滑曲线坐标点

Chaikin算法——计算折线对应的平滑曲线坐标点 本文将介绍一种计算折线对应的平滑曲线坐标点的算法。该算法使用Chaikin曲线平滑处理的方法&#xff0c;通过控制张力因子和迭代次数来调整曲线的平滑程度和精度。通过对原始点集合进行切割和插值操作&#xff0c;得到平滑的曲线坐…

【原创】解决Kotlin无法使用@Slf4j注解的问题

前言 主要还是辟谣之前的网上的用法&#xff0c;当然也会给出最终的使用方法。这可是Kotlin&#xff0c;关Slf4j何事&#xff01;&#xff1f; 辟谣内容&#xff1a;创建注解来解决这个问题 例如&#xff1a; Target(AnnotationTarget.CLASS) Retention(AnnotationRetentio…

开源Linux社区Armbian开发指南

1. 什么是armbian Armbian是一个基于Debian或Ubuntu的开源操作系统&#xff0c;专门针对嵌入式ARM平台进行优化和定制。Armbian可以运行在多种不同的嵌入式设备上&#xff0c;例如树莓派、ArmSoM、香蕉派等等。Armbian针对不同的嵌入式平台&#xff0c;提供了相应的硬件支持&a…

利用HTTP2,新型DDoS攻击峰值破纪录

亚马逊、Cloudflare 和谷歌周二联合发布消息称&#xff0c;一种依赖于 HTTP/2 快速重置技术的攻击行为对它们造成了破纪录的分布式拒绝服务 (DDoS) 攻击。 根据披露的信息&#xff0c;该攻击自8月下旬以来便一直存在&#xff0c;所利用的漏洞被跟踪为CVE-2023-44487&#xff0c…

基于SpringBoot+Vue的服装销售系统

基于SpringBootVue的服装销售平台的设计与实现~ 开发语言&#xff1a;Java数据库&#xff1a;MySQL技术&#xff1a;SpringBootMyBatisVue工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 系统展示 主页 我的订单 登录界面 管理员界面 摘要 基于SpringBoot和Vue的服装销售系统…

管理类联考——数学——汇总篇——知识点突破——数据分析——记忆

文章目录 考点记忆/考点汇总——按大纲 整体目录大纲法记忆宫殿法绘图记忆法 局部数字编码法对号不对号 归类记忆法重点记忆法歌决记忆法口诀&#xff1a;加法分类&#xff0c;类类相加&#xff1b;乘法分步&#xff0c;步步相乘。 谐音记忆法涂色 理解记忆法比较记忆法转图像记…

Qwt开发环境搭建(保姆级教程)

1.简介 QWT&#xff0c;即Qt Widgets for Technical Applications&#xff0c;其目标是以基于2D方式的窗体部件来显示数据&#xff0c; 数据源以数值&#xff0c;数组或一组浮点数等方式提供&#xff0c; 输出方式可以是Curves&#xff08;曲线&#xff09;&#xff0c;Slider…

计算机毕设 opencv 图像识别 指纹识别 - python

文章目录 0 前言1 课题背景2 效果展示3 具体实现3.1 图像对比过滤3.2 图像二值化3.3 图像侵蚀细化3.4 图像增强3.5 特征点检测 4 OpenCV5 最后 0 前言 &#x1f525; 这两年开始毕业设计和毕业答辩的要求和难度不断提升&#xff0c;传统的毕设题目缺少创新和亮点&#xff0c;往…

liunx Centos-7.5上 rabbitmq安装

在安装rabbitmq中需要注意&#xff1a; 1、rabbitmq依赖于erlang&#xff0c;需要先安装erlang 2、erlang和rabbitmq版本有对应关系 可参考网页&#xff1a;https://www.rabbitmq.com/which-erlang.html 第一步&#xff0c;安装编译工具及库文件,如果服务器上已经有了&…

如何在vscode中添加less插件

Less &#xff08;Leaner Style Sheets 的缩写&#xff09; 是一门向后兼容的 CSS 扩展语言。它对CSS 语言增加了少许方便的扩展&#xff0c;通过less可以编写更少的代码实现更强大的样式。但less不是css&#xff0c;浏览器不能直接识别&#xff0c;即浏览器无法执行less代码&a…

【mediasoup-sfu-cpp】3: SfuDemo:加入会议首次成功运行

【mediasoup-sfu-cpp】2:SfuCppDemo 和MediaSoup实例 可以发现闫华大神的demo是开箱即用的。虽然客户端的demo 未开源,但是是可以测试的。正确加入后应该就是发布了视频的 加入会议后默认开启camera,ID 是自己填写的,代表UID demo自己随机生成就可以。 配置本地服务地址 ws…

场效应管器件

在面试硬件方面的工作时&#xff0c;我们通常会被提问模电方面的知识。 场效应管简称FET,有三级&#xff1a;源极(S)、漏极(D)、栅极&#xff08;G&#xff09;&#xff1b;可以实现电压控制电流源&#xff1b;“源极和漏极之间的漏极电流Id&#xff0c;由栅极的负电压进行控制…

轻量级仿 Spring Boot=嵌入式 Tomcat+Spring MVC

啥&#xff1f;Spring Boot 不用&#xff1f;——对。就只是使用 Spring MVC Embedded Tomcat&#xff0c;而不用 Boot。为啥&#xff1f;——因为 Boot 太重了&#xff1a;&#xff09; 那是反智吗&#xff1f;Spring Boot 好好的就只是因为太重就不用&#xff1f;——稍安勿…

刀片式服务器介绍

大家都知道服务器分为机架式服务器、刀片式服务器、塔式服务器三类&#xff0c;今天小编就分别讲一讲这三种服务器&#xff0c;第二篇先来讲一讲刀片式服务器的介绍。 刀片式服务器定义&#xff1a;是一种高密度的服务器架构&#xff0c;通过多个独立服务器单元组成&#xff0c…

windows qemu安装飞腾Aarch64 操作系统 亲测

在win7&#xff08;X86架构CPU&#xff09;下使用QEMU虚拟机运行银河麒麟操作系统&#xff08;ARM架构CPU&#xff09; 1、下载并安装QEMU虚拟机软件 https://qemu.weilnetz.de/w64/2020/ 2、准备好ARM银河麒麟操作系统.iso文件 这里是 Kylin-Desktop-V10-Release-2107-ar…

力扣:141. 环形链表(Python3)

题目&#xff1a; 给你一个链表的头节点 head &#xff0c;判断链表中是否有环。 如果链表中有某个节点&#xff0c;可以通过连续跟踪 next 指针再次到达&#xff0c;则链表中存在环。 为了表示给定链表中的环&#xff0c;评测系统内部使用整数 pos 来表示链表尾连接到链表中的…

CentOS 编译安装 nginx

CentOS 编译安装 nginx 修改 yum 源地址为 阿里云 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repoyum makecache升级内核和软件 yum -y update安装常用软件和依赖 yum -y install gcc gcc-c make cmake zlib zlib-devel openss…
最新文章