Mysql入门练习题
1、在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
mysql> select name,age from students where age>25 and gender='M';
+---------------+-----+
| name | age |
+---------------+-----+
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Yu Yutong | 26 |
| Shi Qing | 46 |
| Tian Boguang | 33 |
| Xu Xian | 27 |
| Sun Dasheng | 100 |
| Song Jiang | 45 |
| Zhang Sanfeng | 94 |
+---------------+-----+
9 rows in set (0.00 sec)
2、以ClassID为分组依据,显示每组的平均年龄
mysql> select classid,avg(age) from students group by classid;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| 2 | 36.0000 |
| 1 | 20.5000 |
| 4 | 24.7500 |
| 3 | 20.2500 |
| 5 | 46.0000 |
| 7 | 19.6667 |
| 6 | 20.7500 |
| NULL | 58.2500 |
+---------+----------+
8 rows in set (0.00 sec)
3、显示第2题中平均年龄大于30的分组及平均年龄
mysql> select classid,avg(age) from students group by classid having avg(age)>30;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| 2 | 36.0000 |
| 5 | 46.0000 |
| NULL | 58.2500 |
+---------+----------+
3 rows in set (0.00 sec)
4、显示以L开头的名字的同学的信息
mysql> select * from students where name like 'l%';
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 27 | li | 10 | F | NULL | NULL |
| 31 | Lin Chaoying | 93 | F | NULL | NULL |
+-------+--------------+-----+--------+---------+-----------+
5 rows in set (0.00 sec)
5、显示TeacherID非空的同学的相关信息
mysql> select * from students where teacherid is not null;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
+-------+-------------+-----+--------+---------+-----------+
5 rows in set (0.00 sec)
6、以年龄排序后,显示年龄最大的前10位同学的信息
mysql> select * from students order by age desc limit 10;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
| 29 | Zhang Sanfeng | 94 | M | NULL | NULL |
| 31 | Lin Chaoying | 93 | F | NULL | NULL |
| 30 | Miejue Shitai | 77 | F | NULL | NULL |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 28 | Song Jiang | 45 | M | NULL | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 24 | Xu Xian | 27 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
10 rows in set (0.00 sec)
7、查询年龄大于等于20岁,小于等于25岁的同学的信息
mysql> select * from students where age>=20 and age<=25;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 26 | wang | 20 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
11 rows in set (0.00 sec)
mysql> select * from students where age between 20 and 25;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 26 | wang | 20 | M | NULL | NULL |
| 33 | hello world | 20 | F | NULL | 1 |
+-------+---------------+-----+--------+---------+-----------+
12 rows in set (0.00 sec)
8、以ClassID分组,显示每班的同学的人数
mysql> select classid,count(*) from students group by classid;
+---------+----------+
| classid | count(*) |
+---------+----------+
| 2 | 3 |
| 1 | 4 |
| 4 | 4 |
| 3 | 4 |
| 5 | 1 |
| 7 | 3 |
| 6 | 4 |
| NULL | 8 |
+---------+----------+
8 rows in set (0.00 sec)
9、以Gender分组,显示其年龄之和
mysql> select gender,sum(age) from students group by gender;
+--------+----------+
| gender | sum(age) |
+--------+----------+
| M | 649 |
| F | 390 |
+--------+----------+
2 rows in set (0.00 sec)
10、以ClassID分组,显示其平均年龄大于25的班级
mysql> select ifnull(classid,'无班级'),avg(age) from students group by classid having avg((age)>25;
+-----------------------------+----------+
| ifnull(classid,'无班级') | avg(age) |
+-----------------------------+----------+
| 2 | 36.0000 |
| 5 | 46.0000 |
| 无班级 | 48.1000 |
+-----------------------------+----------+
3 rows in set (0.00 sec)
11、以Gender分组,显示各组中年龄大于25的学员的年龄之和
mysql> select gender,sum(age) from students where age>25 group by gender;
+--------+----------+
| gender | sum(age) |
+--------+----------+
| M | 433 |
| F | 170 |
+--------+----------+
2 rows in set (0.00 sec
12、显示前5位同学的姓名、课程及成绩
mysql> select name,course,score from students inner join scores on students.stuid=scores.stuid inner join courses on scores.courseid=courses.courseid order by score desc limit 5;
+-------------+----------------+-------+
| name | course | score |
+-------------+----------------+-------+
| Shi Potian | Daiyu Zanghua | 97 |
| Shi Qing | Hamo Gong | 96 |
| Shi Zhongyu | Weituo Zhang | 93 |
| Lin Daiyu | Jinshe Jianfa | 93 |
| Ding Dian | Kuihua Baodian | 89 |
+-------------+----------------+-------+
5 rows in set (0.00 sec)
13、显示其成绩高于80的同学的名称及课程
mysql> select name,score,course from students inner join scores on students.stuid=scores.stuid join courses on scores.courseid=courses.courseid where score>80;
+-------------+-------+----------------+
| name | score | course |
+-------------+-------+----------------+
| Shi Zhongyu | 93 | Weituo Zhang |
| Shi Potian | 97 | Daiyu Zanghua |
| Xie Yanke | 88 | Kuihua Baodian |
| Ding Dian | 89 | Kuihua Baodian |
| Shi Qing | 96 | Hamo Gong |
| Xi Ren | 86 | Hamo Gong |
| Xi Ren | 83 | Dagou Bangfa |
| Lin Daiyu | 93 | Jinshe Jianfa |
+-------------+-------+----------------+
8 rows in set (0.00 sec)
14、取每位同学各门课的平均成绩,显示成绩前三名的同学的姓名和平均成绩
mysql> select name,course,avg(score) from students inner join scores on students.stuid=scores.stuid inner join courses on scores.courseid=courses.courseid group by name,course order by avg(score) desc limit 3;
+------------+---------------+------------+
| name | course | avg(score) |
+------------+---------------+------------+
| Shi Potian | Daiyu Zanghua | 97.0000 |
| Shi Qing | Hamo Gong | 96.0000 |
| Lin Daiyu | Jinshe Jianfa | 93.0000 |
+------------+---------------+------------+
3 rows in set (0.00 sec)
15、显示每门课程课程名称及学习了这门课的同学的个数
mysql> select course,count(name) from students inner join scores on students.stuid=scores.stuid inner join courses on scores.courseid=courses.courseid group by course;
+----------------+-------------+
| course | count(name) |
+----------------+-------------+
| Kuihua Baodian | 4 |
| Weituo Zhang | 2 |
| Daiyu Zanghua | 2 |
| Hamo Gong | 3 |
| Dagou Bangfa | 2 |
| Taiji Quan | 1 |
| Jinshe Jianfa | 1 |
+----------------+-------------+
7 rows in set (0.00 sec)
16、显示其年龄大于平均年龄的同学的名字
mysql> select name,age from students where age>(select avg(age) from students);
+---------------+-----+
| name | age |
+---------------+-----+
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Shi Qing | 46 |
| Tian Boguang | 33 |
| Sun Dasheng | 77 |
| Song Jiang | 45 |
| Zhang Sanfeng | 94 |
| Miejue Shitai | 77 |
| Lin Chaoying | 93 |
+---------------+-----+
9 rows in set (0.00 sec)
17、显示其学习的课程为第1、2,4或第7门课的同学的名字
mysql> select name,classid from students where classid rlike '1|2|4|7';
+--------------+---------+
| name | classid |
+--------------+---------+
| Shi Zhongyu | 2 |
| Shi Potian | 1 |
| Xie Yanke | 2 |
| Ding Dian | 4 |
| Lin Daiyu | 7 |
| Wen Qingqing | 1 |
| Tian Boguang | 2 |
| Duan Yu | 4 |
| Xu Zhu | 1 |
| Lin Chong | 4 |
| Hua Rong | 7 |
| Diao Chan | 7 |
| Xiao Qiao | 1 |
| Ma Chao | 4 |
+--------------+---------+
14 rows in set (0.00 sec)
mysql> select name,classid from students where classid in (1,2,4,7);
+--------------+---------+
| name | classid |
+--------------+---------+
| Shi Zhongyu | 2 |
| Shi Potian | 1 |
| Xie Yanke | 2 |
| Ding Dian | 4 |
| Lin Daiyu | 7 |
| Wen Qingqing | 1 |
| Tian Boguang | 2 |
| Duan Yu | 4 |
| Xu Zhu | 1 |
| Lin Chong | 4 |
| Hua Rong | 7 |
| Diao Chan | 7 |
| Xiao Qiao | 1 |
| Ma Chao | 4 |
+--------------+---------+
14 rows in set (0.00 sec)
18、显示其成员数最少为3个的班级的同学中年龄大于同班同学平均年龄的同学(未理解)
mysql> select s.name,s.classid,s.age,s3.ave from students s left join (select classid,avg(age) ave from students group by classid having count(*)>=3 ) s3 on s.classid=s3.classid where s.classid in (select classid from students group by classid having count(*)>=3) and s.age>s3.ave;
+---------------+---------+-----+---------+
| name | classid | age | ave |
+---------------+---------+-----+---------+
| Shi Potian | 1 | 22 | 20.5000 |
| Ding Dian | 4 | 32 | 24.7500 |
| Yu Yutong | 3 | 26 | 20.2500 |
| Yuan Chengzhi | 6 | 23 | 20.7500 |
| Xu Zhu | 1 | 21 | 20.5000 |
| Lin Chong | 4 | 25 | 24.7500 |
| Hua Rong | 7 | 23 | 19.6667 |
| Huang Yueying | 6 | 22 | 20.7500 |
+---------------+---------+-----+---------+
8 rows in set (0.00 sec)
19、统计各班级中年龄大于全校同学平均年龄的同学
mysql> select name,class,age from students left join classes on students.classid=classes.classid where age>(select avg(age) from students) and class is not null;
+--------------+----------------+-----+
| name | class | age |
+--------------+----------------+-----+
| Ding Dian | Wudang Pai | 32 |
| Shi Qing | Riyue Shenjiao | 46 |
| Tian Boguang | Emei Pai | 33 |
+--------------+----------------+-----+
3 rows in set (0.00 sec)
Mysql入门练习题的更多相关文章
- 21分钟 MySQL 入门教程(转载!!!)
21分钟 MySQL 入门教程 目录 一.MySQL的相关概念介绍 二.Windows下MySQL的配置 配置步骤 MySQL服务的启动.停止与卸载 三.MySQL脚本的基本组成 四.MySQL中的数 ...
- MySQL入门02-MySQL二进制版本快速部署
在上篇文章 MySQL入门01-MySQL源码安装 中,我们介绍了MySQL源码安装的方法. 源码安装虽然有着更加灵活和更加优化等诸多优势.但源码编译安装部署的过程相对复杂,而且整个过程所花费的时间很 ...
- MYSQL入门全套(第三部)
MYSQL入门全套(第一部) MYSQL入门全套(第二部) 索引简介 索引是对数据库表中一个或多个列(例如,employee 表的姓名 (name) 列)的值进行排序的结构.如果想按特定职员的姓来查找 ...
- MySQL入门(三)
写了两篇<MySQL入门>以后我发现,写书的人还是都挺有本事的,起码人家知道怎么编排自己想讲的知识点,我实在是不知道该先说那里后说哪里,那我就想到什么讲什么吧. 一 写SQL 其实我是不想 ...
- python入门练习题1
常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...
- MySQL入门笔记
MySQL入门笔记 版本选择: 5.x.20 以上版本比较稳定 一.MySQL的三种安装方式: 安装MySQL的方式常见的有三种: · rpm包形式 · 通用二进制 ...
- MySQL入门转载
21分钟 MySQL 入门教程 http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html 目录 一.MySQL的相关概念介绍 二.Wi ...
- mysql 入门 基本命令
MYSQL入门学习之一:基本操作 1.登录数据库 www.2cto.com 命令:mysql -u username –p (mysql -h主机地址 -u用户名 -p用户密码) ...
- MySQL入门笔记(一)
一.数据类型 1. 整型 2. 浮点型 3. 字符型 4. 日期时间型 二.数据库操作 1. 创建库 CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_nam ...
随机推荐
- openjdk的bug
容器内就获取个cpu利用率,怎么就占用单核100%了呢 背景:这个是在centos7 + lxcfs 和jdk11 的环境上复现的 下面列一下我们是怎么排查并解这个问题的. 一.故障现象 oppo内核 ...
- NC202475 树上子链
题目链接 题目 题目描述 给定一棵树 T ,树 T 上每个点都有一个权值. 定义一颗树的子链的大小为:这个子链上所有结点的权值和 . 请在树 T 中找出一条最大的子链并输出. 输入描述 第一行输入一个 ...
- C#基础_枚举
一.在学习枚举之前,首先来听听枚举的优点. 1.枚举能够使代码更加清晰,它允许使用描述性的名称表示整数值. 2.枚举使代码更易于维护,有助于确保给变量指定合法的.期望的值. 3.枚举使代码更易输入. ...
- kafka详解(一)--kafka是什么及怎么用
kafka是什么 在回答这个问题之前,我们需要先了解另一个东西--event streaming. 什么是event streaming 我觉得,event streaming 是一个动态的概念,它描 ...
- AI听曲识歌!哼曲、口哨吹,都能秒识! ⛵
作者:韩信子@ShowMeAI 深度学习实战系列:https://www.showmeai.tech/tutorials/42 自然语言处理实战系列:https://www.showmeai.tech ...
- day37-IO流04
JavaIO流04 4.常用的类03 4.4节点流和处理流02 4.4.5对象处理流-ObjectInputStream和ObjectOutputStream 1.序列化和反序列化 例子1: 看一个需 ...
- 通过vNode实现给列表字段打标签
问题 如何给列表数据打标签?类似下面这种样子 思路 数模转化(对接口请求回来的数据进行过滤标记,返回新的数据) 渲染新的数据模型 实现 1.过滤数据,需要打标签的采用jsx写法 业务数据的处理我封装在 ...
- ProxySQL 使用情况报错问题汇总及解决办法
1.ProxySQL Error: connection is locked to hostgroup 2 but trying to reach hostgroup 1 解决方案:登上proxysq ...
- KVM下virtio驱动虚拟机XML配置文件分析
[root@opennebula qemu]# pwd /etc/libvirt/qemu [root@opennebula qemu]# ls networks one-12.xml one-12. ...
- 授予用户/用户组访问 Kubernetes 的一个名称空间
转载地址:https://www.kuboard.cn/learning/k8s-advanced/sec/rbac/auth-namespace.html 前提条件 已安装 Kuboard v3,版 ...