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

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)

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