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.hqwc.cn/news/150824.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hadoop集群搭建

hadoop有三种部署方式 1、Local (Standalone) Mode&#xff08;单机模式&#xff09; 数据存储在本地 2、Pseudo-Distributed Mode&#xff08;伪集群模式&#xff09; 数据存储在HDFS 3、Fully-Distributed Mode&#xff08;集群模式&#xff09; 集群部署&#xff0c;数据存储…

网络扫描与网络监听

前言&#xff1a;前文给大家介绍了网络安全相关方面的基础知识体系&#xff0c;以及什么是黑客&#xff0c;本篇文章笔者就给大家带来“黑客攻击五部曲”中的网络扫描和网络监听 目录 黑客攻击五部曲 网络扫描 按扫描策略分类 按照扫描方式分类 被动式策略 系统用户扫描 …

Matter.js 插件:matter-wrap(世界是圆的)

本文简介 点赞 关注 收藏 学会了 记得以前看爆笑校园里有一集讲到&#xff0c;一个人对着前面开了一枪&#xff0c;过了一阵子弹打中他自己的后脑勺。作者想通过这个冷笑话告诉大家一件事&#xff1a;地球是圆的。 在 Matter.js 世界里&#xff0c;默认是没有边界的&#…

MSQL系列(八) Mysql实战-SQL存储引擎

Mysql实战-SQL存储引擎 前面我们讲解了索引的存储结构&#xff0c;BTree的索引结构&#xff0c;我们一般都知道Mysql的存储引擎有两种&#xff0c;MyISAM和InnoDB,今天我们来详细讲解下Mysql的存储引擎 文章目录 Mysql实战-SQL存储引擎1.存储引擎2.MyISAM的特点3. InnoDB的特…

11 结构型模式- 代理模式

结构性模式一共包括七种&#xff1a; 代理模式、桥接模式、装饰者模式、适配器模式、门面(外观)模式、组合模式、和享元模式。 1 代理模式介绍 软件开发中的代理&#xff1a; 代理模式中引入了一个新的代理对象,代理对象在客户端对象和目标对象之间起到了中介的作用,它去掉客…

Linux系列讲解 —— VIM配置与美化

目录 1. Vim基本配置2. 插件管理器Vundle2.1 下载Vundle2.2 在vimrc中添加Vundle的配置 3. Vundle的使用3.1 安装常用插件3.1.1 NERDTree 3.2 卸载插件 1. Vim基本配置 1.1 配置文件 vim的配置文件有两处&#xff0c;请根据实际情况选择修改哪个。 (1) 全局配置文件&#xff…

RT-Thread 7. RT-Thread Studio ENV修改MCU型号

1. 修改MCU型号 2.在ENV界面输入 scons -c scons --dist3. dist下为更新后完整源代码 4.导入RT-Thread Studio 发现GD32F330已经生效了。 5. 自己编写startup_gd32f3x0.S&#xff0c;准确性待验证 ;/* ; * Copyright (c) 2006-2021, RT-Thread Development Team ; * ; * SPD…

javaEE -10(11000字详解5层重要协议)

一&#xff1a;应用层重点协议 1.1&#xff1a; DNS DNS&#xff0c;即Domain Name System&#xff0c;域名系统。DNS是一整套从域名映射到IP的系统。 TCP/IP中使用IP地址来确定网络上的一台主机&#xff0c;但是IP地址不方便记忆&#xff0c;且不能表达地址组织信息&#x…

c++ qt连接操作sqlite

qt客户端编程,用到数据库的场景不多,但是部分项目还是需要数据库来保存同步数据,客户端用到的数据库,一般是sqlite。 Qt提供了数据库模块,但是qt本身的数据库模块并不好用,会有各种问题, 建议大家不要,可以自己封装数据库的操作。本篇博客介绍qt连接操作sqlite。 sqlit…

【Java 进阶篇】Java Request 继承体系详解

在Java编程中&#xff0c;Request&#xff08;请求&#xff09;是一个常见的概念&#xff0c;特别是在Web开发中。Request通常用于获取来自客户端的信息&#xff0c;以便服务器能够根据客户端的需求提供相应的响应。在Java中&#xff0c;Request通常涉及到一系列类和接口&#…

J2EE项目部署与发布(Windows版本)

&#x1f3ac; 艳艳耶✌️&#xff1a;个人主页 &#x1f525; 个人专栏 &#xff1a;《Spring与Mybatis集成整合》《Vue.js使用》 ⛺️ 越努力 &#xff0c;越幸运。 1.单机项目的部署 1.1们需要将要进行部署的项目共享到虚拟机中 在部署项目之前&#xff0c;我们先要检查一下…

【面试经典150 | 链表】合并两个有序链表

文章目录 Tag题目来源题目解读解题思路方法一&#xff1a;递归方法二&#xff1a;迭代 写在最后 Tag 【递归】【迭代】【链表】 题目来源 21. 合并两个有序链表 题目解读 合并两个有序链表。 解题思路 一种朴素的想法是将两个链表中的值存入到数组中&#xff0c;然后对数组…