MySQL:数据库练习题(附答案)

2023-05-16

#建表内容如下:
表(一)student(学生)

create table student(
    sno varchar(20) primary key not null comment'学号(主码)',
    sname varchar(20) not null comment'学生姓名',
    ssex varchar(20) not null comment'学生性别',
    sbirthday datetime comment'学生出生年月',
    class varchar(20) comment'学生所在班级'
    );

表(二)Course(课程表)

create table course(
    cno varchar(20) primary key not null comment'课程号(主码)',
    cname varchar(20) not null comment'课程名称',
    tno varchar(20) not null comment'教工编号'
);

表(三)Score(成绩表)

create table score(
    id int primary key auto_increment comment'主键自增',
    sno varchar(20) not null comment'学号',
    cno varchar(20) not null comment'课程号',
    degree Decimal(4,1) comment'成绩'
);

表(四) teacher(教师表)

create table teacher(
    tno varchar(20) primary key not null comment'教工编号(主码)',
    tname varchar(20) not null comment'教工姓名',
    tsex varchar(20) not null comment'教工性别',
    tbirthday datetime comment'教工出生年月',
    prof varchar(20) comment'职称',
    depart varchar(20) not null comment'教工所在部门'
);

#插入内容如下:
学生表数据的插入:

insert into student values
('108','曾华','男','1977-09-01','95033'),
('105','匡明','男','1977-09-01','95031'),
('107','王丽','女','1977-09-01','95033'),
('101','李军','男','1977-09-01','95033'),
('109','王芳','女','1977-09-01','95031'),
('103','陆君','男','1977-09-01','95031');

课程表数据的插入:

insert into course values
('3-105','计算机导论','825'),
('3-245','操作系统','804'),
('6-166','数字电路','856'),
('9-888','高等数学','831');

成绩表数据的插入:

select cno from score group by cno; #找出这个表中所有的班级
insert into score(sno,cno,degree) values
('103','3-245','86'),
('105','3-245','75'),
('109','3-245','68'),
('103','3-105','92'),
('105','3-105','88'),
('109','3-105','76'),
('101','3-105','64'),
('107','3-105','91'),
('108','3-105','78'),
('101','6-166','85'),
('107','6-166','79'),
('108','6-166','81');

教师表数据的插入:

insert into teacher values
('804','李诚','男','1958-12-02','副教授','计算机系'),
('856','张旭','男','1969-03-12','讲师','电子工程系'),
('825','王萍','女','1972-05-05','助教','计算机系'),
('831','刘冰','女','1977-08-14','助教','电子工程系');

练习题及答案:
1、 查询Student表中的所有记录的Sname、Ssex和Class列。

  select sname,ssex,class from student;

2、查询教师所有的单位即不重复的Depart列。

select distinct depart  from teacher;

3、 查询Student表的所有记录。

select * from student;

4、 查询Score表中成绩在60到80之间的所有记录。

   select * from score where degree>60 and degree<80;

5、 查询Score表中成绩为85,86或88的记录。

  select * from score where degree in (85,86,88);

6、 查询Student表中“95031”班或性别为“女”的同学记录。

   select * from student where class like '95031' or ssex like '女';

7、 以Class降序查询Student表的所有记录。

  select * from student order by class desc;

8、 以Cno升序、Degree降序查询Score表的所有记录。

 select * from score order by cno asc , degree desc;

9、 查询“95031”班的学生人数。

 select class,count(*) from student where class='95031' group by class;

10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

 select * from score order by degree desc limit 1;
 select max(degree) from score   #先写出score的最高分
 select * from score where degree = (select max(degree) from score);
 select sno,cno from score where degree = (select max(degree) from score);

11、 查询每门课的平均成绩。

select cno,count(*),avg(degree) from score group by cno; 

多条查询
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

方法一:select n.cno,n.num,n.avg from (select count(*) num,cno,avg(degree) avg from score group by cno) n where n.num >=5;
 
方法二:select cno,count(*),avg(degree) from score where cno like '3%' group by cno having count(*) >= 5;

13、查询分数大于70,小于90的Sno列。

select sno,degree from score where degree>70 and degree<90;
select group_concat(sno) from score where degree>70 and degree<90;

14、查询所有学生的Sname、Cno和Degree列。(多表查询)

student.sname,course.cno,score.degree
select student.sname,course.cno,score.degree from student,course,score;
select sname,cno,degree from student join score on student.sno = score.sno;

15、查询所有学生的Sno、Cname和Degree列。

select sno,cname,degree from score join course on course.cno = score.cno;
select student.sname,score.cno,score.degree from score left join student on score.sno=student.sno;

16、查询所有学生的Sname、Cname和Degree列。

student.sname,course.cname,score.degree;

方法一:select student.sname,cname,degree from student join score on student.sno = score.sno join course on course.cno = score.cno;
方法二:select sname,cname,degree from course join (select cno,sname,degree from student join score on student.sno = score.sno) new_table on course.cno = new_table.cno;

17、查询“95033”班学生的平均分。

方法一: select avg(degree) from score where sno in(select sno from student where class = '95033');
方法二:select avg(degree),class from student join score on student.sno = score.sno where class like '95033' group by class;

18、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

select sno,cno,degree from score where cno like '3-105' and degree > (select degree from score where cno like '3-105' and sno like '109');

19、查询score中选学多门课程的同学中分数为非最高分成绩的记录。

select * from score a where Sno in (select Sno from score group by Sno having count(*)>1 )  and Degree not in (select max(Degree) from score a where Sno in (select Sno from score group by Sno having count(*)>1 ));

20、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

select degree from score where sno = '109' and cno = '3-105';
select * from score where degree > (select degree from score where sno = '109' and cno = '3-105');

21、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

select sbirthday from student where sno = '108';
select sno,sname,sbirthday from student where sbirthday = (select sbirthday from student where sno = '108');

22、查询“张旭“教师任课的学生成绩(姓名)。

select tno from teacher where tname = '张旭';    #找出教师编号
select cno from course where tno = (select tno from teacher where tname = '张旭'); #找出课程编号
select sno,degree from score where cno = (select cno from course where tno = (select tno from teacher where tname = '张旭'));
select student.sno,degree,sname from score join student on score.sno = student.sno where student.sno in (select sno from score where cno = (select cno from course where tno = (select tno from teacher where tname = '张旭')));

23、查询考计算机导论的学生成绩

select cno from course where cname = '计算机导论';  #找到课程编号3-105
select sno,degree from score where cno = (select cno from course where cname = '计算机导论');

24、查询李诚老师教的课程名称

 select tno from teacher where tname = '李诚';  ##找到教师编号
 select cname from course where tno = (select tno from teacher where tname = '李诚');

25、教高等数学的老师是哪个系的

select tno from course where cname = '高等数学';
select depart from teacher where tno = (select tno from course where cname = '高等数学');

26、查询选修某课程的同学人数多于5人的教师姓名。

select cno,count(*) from score group by cno having count(*)>=5;   #找出课程编号
select tno from course where cno = (select cno from score group by cno having count(*)>=5);
select tname from teacher where tno = (select tno from course where cno = (select cno from score group by cno having count(*)>=5));

27、查询95033班和95031班全体学生的记录。

 select * from student group by class having count(*);
 select * from student order by class desc;

28、查询成绩表中存在有85分以上成绩的课程Cno.

 select cno,degree from score where degree>85;

29、查询出“计算机系“教师所教课程的成绩表。

 select tno,tname from teacher where depart = '计算机系'   #查出教师编号
 select cno from course where tno in (select tno from teacher where depart = '计算机系');  #查出课程编号
 select sno,cno,degree from score where cno in (select cno from course where tno in (select tno from teacher where depart = '计算机系'));

30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的 Cno、Sno和Degree.

 select max(degree) from score where cno = '3-245'; #先把选修编号为3-245课程的同学的最高成绩查询出来
 select cno,sno,degree from score where cno = '3-105' and degree > (select max(degree) from score where cno = '3-245');

31、查询所有教师和同学的name、sex和birthday.

 select tname as name,tsex as sex,tbirthday as birthday from teacher union select sname,ssex,sbirthday from student;

32、查询所有“女”教师和“女”同学的name、sex和birthday.

 select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex = '女'
    union
    select sname,ssex,sbirthday from student where ssex = '女';

33、查询所有成绩比3-105课程平均成绩低的同学的成绩表。

select avg(degree) from score where cno = '3-105';
select degree from score where degree < (select avg(degree) from score where cno = '3-105');

34、查询所有任课教师的Tname和Depart.

select tname,depart from teacher;

35、查询所有未讲课的教师的Tname和Depart.

 select tno from course group by tno;  #找出有课的老师的编号
 select tname,depart from teacher where not exists(select tno from course group by tno);

36、查询至少有2名男生的班号。

select ssex,class from student where ssex = '男' group by class;
select class from student where exists ((select ssex,class from student where ssex = '男' group by class) * 2);

37、查询Student表中不姓“王”的同学记录。

 select sname from student where sname like '王%'
 select sname from student where sname not in (select sname from student where sname like '王%');

38、查询Student表中每个学生的姓名和年龄。

select sname,floor(datediff(curdate(),sbirthday)/365.2422) from student;
#floor(x)函数:返回小于或等于x的最大整数
#datediff(x,y)函数:计算日期 x->y 之间相隔的天数
#curdate()函数:当前的日期

39、查询Student表中最大和最小的Sbirthday日期值。

select max(sbirthday) as '最大日期' , min(sbirthday) as '最小日期' from student;
update student set sbirthday = '1995-07-11' where sno = '108';
update student set sbirthday = '1820-05-01' where sno = '105';

40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

 select * from student order by class desc,sbirthday desc;

41、查询“男”教师及其所上的课程。

select tno from teacher where tsex = '男';
select cname from course where tno in (select tno from teacher where tsex = '男');

42、查询最高分同学的Sno、Cno和Degree列。

 select max(degree) from score
    select score.sno,cno,degree from student
    join
    score
    on student.sno = score.sno
    where degree = (select max(degree) from score);

43、查询和“李军”同性别的所有同学的Sname.

 select ssex from student where sname = '李军';
    select sname from student where ssex = (select ssex from student where sname = '李军');

44、查询和“李军”同性别并同班的同学Sname.

select class from student where sname = '李军';
select sname from student where ssex = (select ssex from student where sname = '李军') and class = (select class from student where sname = '李军');

45、查询所有选修“计算机导论”课程的“男”同学的成绩表。

 select cno from course where cname = '计算机导论'; #根据课程表找到课程编号
    select sno from score where cno = (select cno from course where cname = '计算机导论'); #根据课程编号找到成绩表里面的学生编号
    select sname from student where sno in (select sno from score where cno = (select cno from course where cname = '计算机导论')) and ssex = '男';

(以上的答案可能会有纰漏,仅供大家参考)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MySQL:数据库练习题(附答案) 的相关文章

  • HTAccess - 令人困惑的干净网址

    如果我想简单地重定向 clients page to clients php view page我会使用像这样简单的东西 它效果很好 Options FollowSymlinks RewriteEngine on RewriteRule c
  • MYSQL中收盘价的简单移动平均线计算和更新表

    我可以使用一些帮助 最好是虚拟指南 来更新下表 CREATE TABLE SYMBOL day date NOT NULL open decimal 8 3 DEFAULT NULL high decimal 8 3 DEFAULT NUL
  • MySQL 与 PHP 的连接无法正常工作

    这是我的情况 我正在尝试使用 Apache 服务器上的 PHP 文件连接到 MySQL 数据库 现在 当我从终端运行 PHP 时 我的 PHP 可以连接到 MySQL 数据库 使用 php f file php 但是当我从网页执行它时 它只
  • MySQL 按主键排序

    某些 SQL 服务器允许使用通用语句 例如ORDER BY PRIMARY KEY 我不相信这适用于 MySQL 是否有任何此类解决方法可以允许跨多个表自动选择 或者是否需要查找查询来确定主键 我一直在研究的解决方法包括调用SHOW COL
  • mysql语句中的*星号是什么意思?

    Ex mysql query SELECT FROM members WHERE id id 这意味着选择表中的所有列
  • C#:SQL 查询生成器类

    在哪里可以找到好的 SQL 查询构建器类 我只需要一个简单的类来构建 SQL 字符串 仅此而已 我需要它用于 C 和 MySql 我真的不需要像 Linq 或 NHibernate 这样的东西 谢谢 由于 Google 将我引导至此页面 我
  • 通过连接从两个表中删除?

    我有两个表如下 tbl1 tbl2 id article id title image whole news tags author older datetime 其中 tbl1 id gt tbl2 article id 如何从两个表中删
  • 使用按位函数查询 BIT 字段时,MySQL 不使用索引

    我的 MySQL 表中有一个 BIT 类型的字段 我想使用位值存储记录的状态 例如 1 status1 2 status2 4 status3 8 status4 每条记录可以同时具有多种状态 对于 status1 和 status3 该值
  • 如何以最少的查询次数获取帖子列表和关联标签

    我的表格结构如下 标签 更多的是一个类别 id 标签名称 描述 slug POSTS ID 标题 网址 邮戳 id idPost idTag USERS ID 用户名 userSlug VOTES id idPost idUser 每个帖子
  • MySQL 存储过程将值分配给 select 语句中的多个变量

    这是我的存储过程 我在为声明的变量赋值时遇到问题 当我执行它时 插入和更新命令工作正常 但声明变量的值保持为 0 但我在数据库中有一些价值 我怎样才能正确地做到这一点 BEGIN DECLARE PaidFee INT DEFAULT 0
  • ActiveRecord3死锁重试

    Rails 3 或 ActiveRecord 3 是否有任何插件可以复制旧版本死锁重试 http agilewebdevelopment com plugins deadlock retry插入 或者 该插件仍然适用于 Rails 3 吗
  • 使 pdo::query 静态

    当我运行下面的代码时出现此错误 我通常使用 msql 函数 但我尝试使用 PDO 代替 怎么了 致命错误 第 14 行无法静态调用非静态方法 PDO query
  • 查询中列的顺序重要吗?

    当从 MySQL 表中选择列时 与表中的顺序相比 选择列的顺序是否会影响性能 不考虑可能覆盖列的索引 例如 您有一个包含行 uid name bday 的表 并且有以下查询 SELECT uid name bday FROM table M
  • MYSQL - 查找最近的前一天

    我可以以某种方式 不使用 PHP 找到一周中最近的前一天日期吗 Like 最近的上一个星期二的日期是哪一天 CURDATE INTERVAL WEEKDAY CURDATE wday IF WEEKDAY CURDATE gt wday 0
  • 阻止注销页面后的后退按钮

    我有 php 注销页面 当用户单击注销链接时 请参阅此页面并重定向到索引页面 但是当单击后退按钮时 我会看到带有用户数据的上一页 当然 当我刷新页面时 我看不到以前的页面和数据 我在单击注销并单击后退按钮后检查了其他代码 drupal 但我
  • MySQL Connector/C++ 库链接错误问题

    PROBLEM 好吧 我一直在尝试遵循 MySQL Forge Wiki 和其他一些网站上的示例代码 这些网站提供了有关如何获得简单数据库连接的教程 但由于某种原因 我的项目总是因链接错误而失败 我可以我自己不明白为什么或如何解决它 我仍在
  • 为什么我的 if 语句没有按我预期的方式工作?

    我正在尝试实现以下目标 我向我的 SQL 数据库询问使用SELECT FROM subjects 这样做之后我要求使用数组mysqli fetch assoc 在那之前一切都很好 现在的问题是 当我尝试在每个循环中修改 genero 的值
  • 无法通过套接字“/var/lib/mysql/mysql.sock”连接到本地 MySQL 服务器 (2)

    当我尝试连接 mysql 时出现以下错误 Can t connect to local MySQL server through socket var lib mysql mysql sock 2 这个错误有解决办法吗 其背后的原因可能是什
  • MySQL MIN/MAX 所有行

    我有桌子Races与行ID Name and TotalCP 我选择分钟 TotalCP FROM Races 但是我想选择具有最小值的整行 我如何在单个查询中做到这一点 从聚合值获取整行的一般形式是 SELECT FROM Races W
  • 忽略重复条目并在 EF Core 中的 DbContext.SaveChanges() 上提交成功条目

    我有一个 ASP Net Core 2 2 Web API 在我的一个控制器操作中 我向 MySQL 数据库表添加了一堆行 我使用的是 Pomelo 例如 dbContext AddRange entities dbContext Save

随机推荐

  • 升级笔记本硬件并安装Debian的过程

    前面说过了手头有个Win10笔记本 xff0c 华硕顽石FL8000U型号 xff0c 4G DDR4 2400MHz 威刚内存 xff0c 1T机械硬盘 xff0c 比较鸡肋 xff0c 开机速度比较慢 xff0c Win10登录界面还会
  • 转载:解决wps linux中字体缺失以及文字体名字全是英文的问题

    第一部分 xff1a 字体缺失问题 可能是版权原因 xff0c Linux中许多字体是没有的 xff0c 可以从Windows系统的Windows Fonts文件夹中 xff0c 复制所需的ttf字体文件到Linux系统的 usr shar
  • 无线键鼠接收器配对怎么就那么难?简直就是浪费

    手头无线键鼠套装 无线鼠标等设备比较多 xff0c 基本每台笔记本都有一套 品牌主要以雷柏和罗技为主 xff0c 罗技主要是鼠标 xff0c 此文不涉及 有一套雷柏的无线键鼠套装 xff0c 因为使用多年 xff0c 鼠标失灵已经扔了 xf
  • 亲测有效的下载微信公众号推文视频的一种方法。

    微信公众号推送的内容也有比较精华的 xff0c 特别是一些演讲视频 与其收藏积灰 xff0c 还不如下载保存 以此篇公众号推文为例 xff1a https mp weixin qq com s qkXLmTqBSAox3gx1yJWsNQ
  • Debian8 jessie 的官方源(备份)

    在非常旧的笔记本上安装了Debian8 然后为了安装速度 xff0c 取消了网络镜像源 又因为中文乱码问题 xff0c 没法更新语言包 加了阿里云 清华 网易等等的jessie软件源后 xff0c 都提示有问题 xff0c 怀疑是不是不支持
  • 更改LXDE的语言为中文

    之前给旧笔记本安装了Debian8 43 LXDE嘛 xff0c 运行比较流畅 安装过程中本想选择中文的 xff0c 但是安装界面中旧有中文乱码 xff0c 所以还是选了英语 等装好了系统 xff0c 把apt update 43 upgr
  • nn.ModuleList和nn.Sequential的理解

    nn ModuleList和nn Sequential的出现是为了使代码简洁 nn ModuleList使对于加入其中的子模块 xff0c 不必在forward中依次调用 nn Sequentialt使对于加入其中的子模块在forward中
  • 红米K40 Pro+5G手机出现电量显示故障的修复

    前两天的某天早晨 xff0c 插了一夜充电器的红米手机电量竟然依然是27 xff0c 比昨晚充电时更少了 摸摸手机也凉凉的 xff0c 看来充电器并未工作 然后就心慌慌的带着手机开了省点模式去上班 xff0c 一路上正常 到了单位后 xff
  • Debian安装myBase8.2 Beta-10的各种踩坑

    先去官网下载Linux的压缩包 xff0c 官网下载页面 for Linux amd64 Ver 8 2 Beta 10 tar xz32MB 然后放到home目录下 xff0c 解压 tar xJf myBase tar xz 得到一个M
  • ArchLinux的安装

    安装虚拟机的时候报了一个错误 xff1a this kernel requires an x86 64 cpu but 该原因 xff1a 操作系统是32位 xff0c 虚拟机的系统镜像是64位的虚拟机配置问题电脑没有开启虚拟化 我在安装的
  • iOS 第13课 使用XIB可视化编辑界面

    xcode XIB 基础 0 和android 里面的preview 一样 xff0c 可以看到同时操作添加和修改视图的位置 1 有一点和 android 比较像的是设置主视图 xff0c 我们可以在我们的项目点击 xff0c 然后是 Ma
  • java excel设置row 里面的宽度自适应

    row 61 sheet createRow i 43 1 HSSFCellStyle cellStyle 61 wb createCellStyle cellStyle setWrapText true row setRowStyle c
  • 看完就会----VSCode配置环境(gcc编译器)

    文章目录 一 xff0c 前言二 xff0c 配置的心酸过程1 xff0c 先下载Mingw642 xff0c 解析一下方框的内容3 xff0c 下载解压和配置 三 xff0c 结束语 一 xff0c 前言 当我们下载完了VSCode运行C
  • vue项目中 使用 websocket

    Websocket是一个持久化的协议 xff0c HTTP是不支持持久连接的 Websocket其实是一个新协议 xff0c 跟HTTP协议基本没有关系 xff0c 只是为了兼容现有浏览器的握手规范而已 比如说我们现在有个需求 实时获取一个
  • SSH客户端常用工具SecureCRT操作

    1 1 SecureCRT工具介绍 SecureCRT是一款支持SSH xff08 SSH1和SSH2 xff09 协议的终端仿真软件 xff0c 常被用来 运行于Windows下远程登录UNIX或Linux服务器 SecureCRT软件功
  • ubuntu apt-get update 失败解决。

    当运行apt get update后出现如下错误时 xff1a E Some index files failed to download they have been ignored or old ones used instead 可以
  • windows环境下定时进行阿里云DDNS解析

    前言 在国内想要申请公网的IPV4 xff0c 机会渺茫 xff0c 不过还好 xff0c 现在三大运营商基本上都已经提供了IPV6了 xff0c 等于给我们留了另外一条路 xff0c 这次在做映射时 xff0c 从网上找了些资料 xff0
  • Visual Studio开发工具----Devenv命令行

    2 Devenv 命令行 借助 Devenv xff0c 可以设置关于IDE 生成项目 调试项目和使用命令行部署项目的各种选项 使用这些开关 xff0c 可以通过脚本或 bat 文件 xff08 如每日构建版脚本 xff09 运行IDE x
  • MySQL:ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list c

    mysql span class token operator gt span span class token keyword select span cno span class token punctuation span span
  • MySQL:数据库练习题(附答案)

    建表内容如下 xff1a 表 xff08 一 xff09 student xff08 学生 span class token keyword create span span class token keyword table span s