mysql grammar exercise 1

1. In the students table, query the name and age of students who are older than 25 years old and are male

MariaDB [hellodb]> select name,age from students where gender='M' and age > 25;
+--------------+-----+
| name         | age |
+--------------+-----+
| Xie Yanke    |  53 |
| Ding Dian    |  32 |
| Yu Yutong    |  26 |
| Shi Qing     |  46 |
| Tian Boguang |  33 |
| Xu Xian      |  27 |
| Sun Dasheng  | 100 |
+--------------+-----+
7 rows in set (0.000 sec)

2. Using ClassID as the grouping basis, display the average age of each group

MariaDB [hellodb]> select classid,avg(age) as 平均年龄  from students group by classid;
+---------+--------------+
| classid | 平均年龄     |
+---------+--------------+
|    NULL |      63.5000 |
|       1 |      20.5000 |
|       2 |      36.0000 |
|       3 |      20.2500 |
|       4 |      24.7500 |
|       5 |      46.0000 |
|       6 |      20.7500 |
|       7 |      19.6667 |
+---------+--------------+
8 rows in set (0.001 sec)

3. Show the group and average age of the average age greater than 30 in question 2

MariaDB [hellodb]> select classid,avg(age) as 平均年龄  from students group by classid having avg(age)> 30;
+---------+--------------+
| classid | 平均年龄     |
+---------+--------------+
|    NULL |      63.5000 |
|       2 |      36.0000 |
|       5 |      46.0000 |
+---------+--------------+
3 rows in set (0.001 sec)

4. Display information of classmates whose names begin with L

MariaDB [hellodb]> 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 |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.000 sec)

5. Display relevant information of students whose TeacherID is not empty

MariaDB [hellodb]> 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.000 sec)

6. After sorting by age, display the information of the top 10 oldest classmates

MariaDB [hellodb]> select * from students order by age desc limit 10;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name         | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng  | 100 | M      |    NULL |      NULL |
|     3 | Xie Yanke    |  53 | M      |       2 |        16 |
|     6 | Shi Qing     |  46 | M      |       5 |      NULL |
|    13 | Tian Boguang |  33 | M      |       2 |      NULL |
|     4 | Ding Dian    |  32 | M      |       4 |         4 |
|    24 | Xu Xian      |  27 | M      |    NULL |      NULL |
|     5 | Yu Yutong    |  26 | M      |       3 |         1 |
|    17 | Lin Chong    |  25 | M      |       4 |      NULL |
|    23 | Ma Chao      |  23 | M      |       4 |      NULL |
|    18 | Hua Rong     |  23 | M      |       7 |      NULL |
+-------+--------------+-----+--------+---------+-----------+
10 rows in set (0.000 sec)

7. Query information about students whose age is greater than or equal to 20 years old and less than or equal to 25 years old

MariaDB [hellodb]> 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 |
+-------+---------------+-----+--------+---------+-----------+
10 rows in set (0.000 sec)

MariaDB [hellodb]> 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 |
+-------+---------------+-----+--------+---------+-----------+
10 rows in set (0.000 sec)

8. Group by ClassID to show the number of students in each class

MariaDB [hellodb]> select classid,count(stuid) 人数 from students group by classid;
+---------+--------+
| classid | 人数   |
+---------+--------+
|    NULL |      2 |
|       1 |      4 |
|       2 |      3 |
|       3 |      4 |
|       4 |      4 |
|       5 |      1 |
|       6 |      4 |
|       7 |      3 |
+---------+--------+
8 rows in set (0.001 sec)

9. Group by Gender and display the sum of their ages

MariaDB [hellodb]> select gender,sum(age) from students group by gender;
+--------+----------+
| gender | sum(age) |
+--------+----------+
| F      |      190 |
| M      |      495 |
+--------+----------+
2 rows in set (0.001 sec)

10. Group by ClassID to show the classes whose average age is greater than 25

MariaDB [hellodb]> select classid,avg(age)  平均年龄 from students group by classid having 平均年龄 > 25;
+---------+--------------+
| classid | 平均年龄     |
+---------+--------------+
|    NULL |      63.5000 |
|       2 |      36.0000 |
|       5 |      46.0000 |
+---------+--------------+

11. Group by Gender, display the sum of the ages of students who are older than 25 in each group

MariaDB [hellodb]> select gender,sum(age) from students where age > 25 group by gender;
+--------+----------+
| gender | sum(age) |
+--------+----------+
| M      |      317 |
+--------+----------+
1 row in set (0.000 sec)

12. Display the names, courses and grades of the top 5 students

MariaDB [hellodb]> select name,course,score from (select * from students limit 5) as st  inner join scores sc on st.stuid=sc.stuid inner join courses co on sc.courseid=co.courseid ;
+-------------+----------------+-------+
| name        | course         | score |
+-------------+----------------+-------+
| Shi Zhongyu | Kuihua Baodian |    77 |
| Shi Zhongyu | Weituo Zhang   |    93 |
| Shi Potian  | Kuihua Baodian |    47 |
| Shi Potian  | Daiyu Zanghua  |    97 |
| Xie Yanke   | Kuihua Baodian |    88 |
| Xie Yanke   | Weituo Zhang   |    75 |
| Ding Dian   | Daiyu Zanghua  |    71 |
| Ding Dian   | Kuihua Baodian |    89 |
| Yu Yutong   | Hamo Gong      |    39 |
| Yu Yutong   | Dagou Bangfa   |    63 |
+-------------+----------------+-------+
10 rows in set (0.001 sec)

13. Show the names and courses of students whose grades are higher than 80

MariaDB [hellodb]> select name,course,score from students as st,(select * from scores where score > 80) as sc ,courses as co where st.stuid=sc.stuid and sc.courseid=co.courseid;
+-------------+----------------+-------+
| name        | course         | Score |
+-------------+----------------+-------+
| Shi Zhongyu | Weituo Zhang   |    93 |
| Shi Potian  | Daiyu Zanghua  |    97 |
| Xie Yanke   | Kuihua Baodian |    88 |
| Ding Dian   | Kuihua Baodian |    89 |
| Shi Qing    | Hamo Gong      |    96 |
| Xi Ren      | Hamo Gong      |    86 |
| Xi Ren      | Dagou Bangfa   |    83 |
| Lin Daiyu   | Jinshe Jianfa  |    93 |
+-------------+----------------+-------+
8 rows in set (0.001 sec)

14. Take the average score of each class of each student, and display the names and average scores of the top three students

MariaDB [hellodb]> select name,avg(score) from students as st,scores as sc,courses as co where st.stuid=sc.stuid and sc.courseid=co.courseid group by st.name order by avg(score) desc limit 3;
+-------------+------------+
| name        | avg(score) |
+-------------+------------+
| Shi Qing    |    96.0000 |
| Shi Zhongyu |    85.0000 |
| Xi Ren      |    84.5000 |
+-------------+------------+
3 rows in set (0.001 sec)

15. Show the name of each course and the number of students who have studied this course

MariaDB [hellodb]> select course,count(stuid) from courses as co,students as st,coc where co.courseid=coc.courseid and st.classid=coc.classid group by course;
+----------------+--------------+
| course         | count(stuid) |
+----------------+--------------+
| Dagou Bangfa   |            4 |
| Daiyu Zanghua  |            8 |
| Hamo Gong      |            5 |
| Jinshe Jianfa  |            7 |
| Kuihua Baodian |           11 |
| Taiji Quan     |            7 |
| Weituo Zhang   |            3 |
+----------------+--------------+
7 rows in set (0.001 sec)

16. Show the names of classmates whose age is greater than the average age

MariaDB [hellodb]> 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  | 100 |
+--------------+-----+
5 rows in set (0.000 sec)

17. Show the name of the classmate whose course is the first, second, fourth or seventh course

MariaDB [hellodb]> select name,courseid from students st,(select * from coc where courseid in (1,2,4,7)) co where  st.classid=co.classid; 
+---------------+----------+
| name          | CourseID |
+---------------+----------+
| Shi Zhongyu   |        2 |
| Shi Potian    |        2 |
| Xie Yanke     |        2 |
| Ding Dian     |        2 |
| Yu Yutong     |        1 |
| Yu Yutong     |        7 |
| Shi Qing      |        1 |
| Xi Ren        |        1 |
| Xi Ren        |        7 |
| Lin Daiyu     |        4 |
| Ren Yingying  |        4 |
| Yue Lingshan  |        1 |
| Yue Lingshan  |        7 |
| Yuan Chengzhi |        4 |
| Wen Qingqing  |        2 |
| Tian Boguang  |        2 |
| Lu Wushuang   |        1 |
| Lu Wushuang   |        7 |
| Duan Yu       |        2 |
| Xu Zhu        |        2 |
| Lin Chong     |        2 |
| Hua Rong      |        4 |
| Xue Baochai   |        4 |
| Diao Chan     |        4 |
| Huang Yueying |        4 |
| Xiao Qiao     |        2 |
| Ma Chao       |        2 |
+---------------+----------+
27 rows in set (0.001 sec)

MariaDB [hellodb]> select distinct name from students st,(select * from coc where courseid in (1,2,4,7)) co where  st.classid=co.classid; 
+---------------+
| name          |
+---------------+
| Shi Zhongyu   |
| Shi Potian    |
| Xie Yanke     |
| Ding Dian     |
| Yu Yutong     |
| Shi Qing      |
| Xi Ren        |
| Lin Daiyu     |
| Ren Yingying  |
| Yue Lingshan  |
| Yuan Chengzhi |
| Wen Qingqing  |
| Tian Boguang  |
| Lu Wushuang   |
| Duan Yu       |
| Xu Zhu        |
| Lin Chong     |
| Hua Rong      |
| Xue Baochai   |
| Diao Chan     |
| Huang Yueying |
| Xiao Qiao     |
| Ma Chao       |
+---------------+
23 rows in set (0.000 sec)

MariaDB [hellodb]> select distinct name from students st,(select * from coc where courseid in (1,2,4,7)) co where  st.classid=co.classid order by name; 
+---------------+
| name          |
+---------------+
| Diao Chan     |
| Ding Dian     |
| Duan Yu       |
| Hua Rong      |
| Huang Yueying |
| Lin Chong     |
| Lin Daiyu     |
| Lu Wushuang   |
| Ma Chao       |
| Ren Yingying  |
| Shi Potian    |
| Shi Qing      |
| Shi Zhongyu   |
| Tian Boguang  |
| Wen Qingqing  |
| Xi Ren        |
| Xiao Qiao     |
| Xie Yanke     |
| Xu Zhu        |
| Xue Baochai   |
| Yu Yutong     |
| Yuan Chengzhi |
| Yue Lingshan  |
+---------------+
23 rows in set (0.001 sec)

18. Show the classmates of the class with at least 3 members who are older than the average age of the classmates

MariaDB [hellodb]> select st.name,st.age,avg.平均年龄 from students st,(select classid,count(*),avg(age) 平均年龄 from students group by classid having count(*)>=3) avg where st.classid=avg.classid and st.age > avg.平均年龄;
+---------------+-----+--------------+
| name          | age | 平均年龄     |
+---------------+-----+--------------+
| Shi Potian    |  22 |      20.5000 |
| Xie Yanke     |  53 |      36.0000 |
| Ding Dian     |  32 |      24.7500 |
| Yu Yutong     |  26 |      20.2500 |
| Yuan Chengzhi |  23 |      20.7500 |
| Xu Zhu        |  21 |      20.5000 |
| Lin Chong     |  25 |      24.7500 |
| Hua Rong      |  23 |      19.6667 |
| Huang Yueying |  22 |      20.7500 |
+---------------+-----+--------------+
9 rows in set (0.001 sec)

19. Count students who are older than the average age of all students in each class

MariaDB [hellodb]> select * from students where age > (select avg(age) from students) and classid is not null;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name         | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
|     3 | Xie Yanke    |  53 | M      |       2 |        16 |
|     4 | Ding Dian    |  32 | M      |       4 |         4 |
|     6 | Shi Qing     |  46 | M      |       5 |      NULL |
|    13 | Tian Boguang |  33 | M      |       2 |      NULL |
+-------+--------------+-----+--------+---------+-----------+
4 rows in set (0.000 sec)

Guess you like

Origin blog.csdn.net/weixin_50904580/article/details/112344250