首页 > 数据库 >Mysql入门练习题

Mysql入门练习题

时间:2022-08-25 22:01:10浏览次数:77  
标签:练习题 classid 入门 students age Mysql NULL select name

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)

标签:练习题,classid,入门,students,age,Mysql,NULL,select,name
From: https://www.cnblogs.com/wdy001/p/16625881.html

相关文章

  • MySql数据库
    MySql数据库概述MySQL是一个基于Sql结构化查询语言的关系型数据库,由瑞典MySQLAB公司开发,目前属于Oracle公司。数据类型数值类型inttinyint、smallint、mediumi......
  • Mysql——索引
    索引1.建立起一个在存储表阶段就有的一个存储结构能在查询的时候加速。2.提高读的速度,降低写的速度。3.读写比例为10:1索引原理:因软件时听过操作系统来进行对硬盘......
  • mysql增删改查
    #include<stdio.h>#include<stdlib.h>#include<mysql.h>#include<unistd.h>#include<string.h>intmain(){ MYSQL*conn; MYSQL_RES*res; MYSQL_ROWro......
  • mysql的增删改查
    针对库的增删改查(文件夹)#增createdatabasedb1;创建一个库库名为db1createdatabasedb2charset='gbk';创建一个库库名为db2charset设置编码为gbk#查sho......
  • jmeter-从入门到精通-定时器2
    什么是定时器?模拟用户思考的时间,,让性能测试更加逼真;JMeter中的定时器一般被我们用来设置延迟与同步。定时器的执行优先级高于Sampler(取样器),在同一作用域(例如控制器下)下有......
  • elasticsearch入门
    1.HTTP操作幂等性和非幂等性的概念:幂等通俗来说是指不管进行多少次重复操作,都是实现相同的结果。在REST风格的请求中,GET,PUT,DELETE都是幂等性操作,而POST不是。1.索引操......
  • Mysql中空间字段类型Wkt和Wkb的坑
    工作中遇到空间数据,提供给我们的是二进制的Wkt的格式,存储在Mysql数据库中,采集的时候告知我们是Wkb的数据格式,由于第一次接触这样的数据格式,去查了官方文档https://www.mys......
  • Java Servlet 入门:问题系列:反射方法参数名获取不到问题:arg0,arg1
    问题:获取反射的方法参数名时,得到arg0,arg1,而不是定义的参数名。示例代码:Parameter[]parameters=methodInfo.getParameters();if(parameters!=null&&parame......
  • MySQL搭建主从集群详细步骤~
    一、Docker安装MySQL搭建主从dockerrun[OPTIONS]IMAGE[COMMAND][ARG...]dockerrun-p3306:3306很多-d--namehahamysql:5.7Docker启动容器的数据......
  • Java 连接 MySQL
    让Java和MySQL连接起来-囧雪诺-博客园 https://www.cnblogs.com/jonsnow/p/6246131.htmlJava连接MySQL需要驱动包,可以下载菜鸟教程提供的 jar包:http://stati......