MariaDB [hellodb]>select * from students;
+-------+---------------+-----+--------+---------+-----------+
| 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 || 6 | Shi Qing | 46 | M | 5 | NULL || 7 | Xi Ren | 19 | F | 3 | NULL || 8 | Lin Daiyu | 17 | F | 7 | NULL || 9 | Ren Yingying | 20 | F | 6 | NULL || 10 | Yue Lingshan | 19 | F | 3 | NULL || 11 | Yuan Chengzhi | 23 | M | 6 | NULL || 12 | Wen Qingqing | 19 | F | 1 | NULL || 13 | Tian Boguang | 33 | M | 2 | NULL || 14 | Lu Wushuang | 17 | F | 3 | NULL || 15 | Duan Yu | 19 | M | 4 | NULL || 16 | Xu Zhu | 21 | M | 1 | NULL || 17 | Lin Chong | 25 | M | 4 | NULL || 18 | Hua Rong | 23 | M | 7 | NULL || 19 | Xue Baochai | 18 | F | 6 | NULL || 20 | Diao Chan | 19 | F | 7 | NULL || 21 | Huang Yueying | 22 | F | 6 | NULL || 22 | Xiao Qiao | 20 | F | 1 | NULL || 23 | Ma Chao | 23 | M | 4 | NULL || 24 | Xu Xian | 27 | M | NULL | NULL || 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows inset(0.001 sec)
1. 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
MariaDB [hellodb]>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 |
+--------------+-----+
7 rows inset(0.000 sec)
2. 以ClassID为分组依据,显示每组的平均年龄
MariaDB [hellodb]>select classid 班级,avg(age) 平均年龄 from students group by 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 inset(0.000 sec)
MariaDB [hellodb]>select classid 班级,avg(age) 平均年龄 from students where classid is not null group by classid;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 1 | 20.5000 || 2 | 36.0000 || 3 | 20.2500 || 4 | 24.7500 || 5 | 46.0000 || 6 | 20.7500 || 7 | 19.6667 |
+--------+--------------+
7 rows inset(0.000 sec)
MariaDB [hellodb]>select classid 班级,avg(age) 平均年龄 from students group by classid having classid is not null;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 1 | 20.5000 || 2 | 36.0000 || 3 | 20.2500 || 4 | 24.7500 || 5 | 46.0000 || 6 | 20.7500 || 7 | 19.6667 |
+--------+--------------+
7 rows inset(0.000 sec)
3. 显示第2题中平均年龄大于30的分组及平均年龄
MariaDB [hellodb]>select classid 班级,avg(age) 平均年龄 from students where classid is not null group by classid having 平均年龄 > 30;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 2 | 36.0000 || 5 | 46.0000 |
+--------+--------------+
2 rows inset(0.000 sec)
MariaDB [hellodb]>select classid 班级,avg(age) 平均年龄 from students group by classid having classid is not null and 平均年龄 > 30;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 2 | 36.0000 || 5 | 46.0000 |
+--------+--------------+
2 rows inset(0.001 sec)
4. 显示以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 inset(0.000 sec)
5. 显示TeacherID非空的同学的相关信息
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 inset(0.000 sec)
6. 以年龄排序后,显示年龄最大的前10位同学的信息
MariaDB [hellodb]>select * from students group 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 || 11 | Yuan Chengzhi | 23 | M | 6 | NULL || 1 | Shi Zhongyu | 22 | M | 2 | 3 |
+-------+---------------+-----+--------+---------+-----------+
10 rows inset(0.000 sec)# sqlserver 中使用top 函数selecttop 10 * from students order by age desc;
7. 查询年龄大于等于20岁,小于等于25岁的同学的信息
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 inset(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 inset(0.001 sec)
MariaDB [hellodb]>select gender 性别,sum(age) from students group by gender;
+--------+----------+
| 性别 | sum(age)|
+--------+----------+
| F | 190 || M | 495 |
+--------+----------+
2 rows inset(0.001 sec)
10. 以ClassID分组,显示其平均年龄大于25的班级
MariaDB [hellodb]>select classid 班级,avg(age) 平均年龄 from students group by classid having avg(age)> 25;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| NULL | 63.5000 || 2 | 36.0000 || 5 | 46.0000 |
+--------+--------------+
3 rows inset(0.001 sec)
MariaDB [hellodb]>select classid 班级,avg(age) 平均年龄 from students where classid is not null group by classid having avg(age)> 25;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 2 | 36.0000 || 5 | 46.0000 |
+--------+--------------+
2 rows inset(0.001 sec)
MariaDB [hellodb]>select classid 班级,avg(age) 平均年龄 from students group by classid having avg(age)> 25 and classid is not null;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 2 | 36.0000 || 5 | 46.0000 |
+--------+--------------+
2 rows inset(0.001 sec)
11. 以Gender分组,显示各组中年龄大于25的学员的年龄之和
MariaDB [hellodb]>select gender,sum(age) over25_sum_age from students where age > 25 group by gender;
+--------+----------------+
| gender | over25_sum_age |
+--------+----------------+
| M | 317 |
+--------+----------------+
1 row inset(0.002 sec)
MariaDB [hellodb]>select name 姓名,course 课程,score 考试成绩 from students st
-> inner join scores sc on st.stuid=sc.stuid
-> inner join courses co on sc.courseid=co.courseid
-> where score > 80;
+-------------+----------------+--------------+
| 姓名 | 课程 | 考试成绩 |
+-------------+----------------+--------------+
| 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 inset(0.001 sec)#另一种方法:
MariaDB [hellodb]>select name 姓名,course 课程,score 考试成绩 from students,scores,courses
-> where students.stuid=scores.stuid and scores.courseid=courses.courseid and score>80;
+-------------+----------------+--------------+
| 姓名 | 课程 | 考试成绩 |
+-------------+----------------+--------------+
| 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 inset(0.001 sec)
14. 取每位同学各门课的平均成绩,显示成绩前三名的同学的姓名和平均成绩
取出每位同学各科平均成绩:
MariaDB [hellodb]>select name 姓名,avg(score) 各科平均成绩 from students st
-> inner join scores sc on st.stuid=sc.stuid
-> group by st.stuid;
+-------------+--------------------+
| 姓名 | 各科平均成绩 |
+-------------+--------------------+
| Shi Zhongyu | 85.0000 || Shi Potian | 72.0000 || Xie Yanke | 81.5000 || Ding Dian | 80.0000 || Yu Yutong | 51.0000 || Shi Qing | 96.0000 || Xi Ren | 84.5000 || Lin Daiyu | 75.0000 |
+-------------+--------------------+
8 rows inset(0.001 sec)
按各科平均成绩排序,取前三名:
MariaDB [hellodb]>select name 姓名,avg(score) 各科平均成绩 from students st
-> inner join scores sc on st.stuid=sc.stuid
-> group by st.stuid order by avg(score) desc limit 3;
+-------------+--------------------+
| 姓名 | 各科平均成绩 |
+-------------+--------------------+
| Shi Qing | 96.0000 || Shi Zhongyu | 85.0000 || Xi Ren | 84.5000 |
+-------------+--------------------+
3 rows inset(0.001 sec)
15. 显示每门课程课程名称及学习了这门课的同学的个数
MariaDB [hellodb]>select course 课程名称,count(course) 学习人数 from students st
-> inner join scores sc on st.stuid=sc.stuid
-> inner join courses co on sc.courseid=co.courseid group by course;
+----------------+--------------+
| 课程名称 | 学习人数 |
+----------------+--------------+
| Dagou Bangfa | 2 || Daiyu Zanghua | 2 || Hamo Gong | 3 || Jinshe Jianfa | 1 || Kuihua Baodian | 4 || Taiji Quan | 1 || Weituo Zhang | 2 |
+----------------+--------------+
7 rows inset(0.001 sec)
16. 显示其年龄大于平均年龄的同学的名字
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 inset(0.000 sec)
17. 显示其学习的课程为第1、2,4或第7门课的同学的名字
MariaDB [hellodb]>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 inset(0.001 sec)
18. 显示其成员数最少为3个的班级的同学中年龄大于同班同学平均年龄的同学
MariaDB [hellodb]>select classid,name,age from students group by classid having count(*)> 3 and age > avg(age);
+---------+------------+-----+
| classid | name | age |
+---------+------------+-----+
| 1 | Shi Potian | 22 || 3 | Yu Yutong | 26 || 4 | Ding Dian | 32 |
+---------+------------+-----+
3 rows inset(0.000 sec)
19. 统计各班级中年龄大于全校同学平均年龄的同学
MariaDB [hellodb]>select classid,name,age from students where age >(select avg(age) from students ) order by classid ;
+---------+--------------+-----+
| classid | name | age |
+---------+--------------+-----+
| NULL | Sun Dasheng | 100 || 2 | Xie Yanke | 53 || 2 | Tian Boguang | 33 || 4 | Ding Dian | 32 || 5 | Shi Qing | 46 |
+---------+--------------+-----+
5 rows inset(0.000 sec)
若classid 为null的不属于本校学生:
MariaDB [hellodb]>select classid,name,age from students where classid is not null and age >(select avg(age) from students where classid is not null ) order by classid;
+---------+--------------+-----+
| classid | name | age |
+---------+--------------+-----+
| 2 | Xie Yanke | 53 || 2 | Tian Boguang | 33 || 3 | Yu Yutong | 26 || 4 | Ding Dian | 32 || 4 | Lin Chong | 25 || 5 | Shi Qing | 46 |
+---------+--------------+-----+
6 rows inset(0.001 sec)