select name,age from students where age>25 and gender='M';
以ClassID为分组依据,显示每组的平均年龄
select classid,avg(age) as 'average age' from students group by classid;
显示第2题中平均年龄大于30的分组及平均年龄
select classid,avg(age) as 'average age' from students group by classid having avg(age)>30;
显示以L开头的名字的同学的信息
select * from students where name like 'L%';
显示TeacherID非空的同学的相关信息
select * from students where teacherid is not null;# 另一种方式(推荐)select * from students where teacherid > 0;
以年龄排序后,显示年龄最大的前10位同学的信息
select * from students order by age desc limit 10;# sqlserver 中使用top 函数,但是MySQL支持这种语法selecttop 10 * from students order by age desc;
查询年龄大于等于20岁,小于等于25岁的同学的信息
select * from students where age>=20 and age<=25;select * from students where age between 20 and 25;
以ClassID分组,显示每班的同学的人数
select classid,count(stuid) as 'sum of students' from students group by classid;
以Gender分组,显示其年龄之和
select gender,sum(age) from students group by gender;
以ClassID分组,显示其平均年龄大于25的班级
select classid,avg(age) from students group by classid having avg(age)>25;
以Gender分组,显示各组中年龄大于25的学员的年龄之和
select gender,sum(age) from students where age>25 group by gender;
显示前5位同学的姓名、课程及成绩
select name,course,score from students inner join scores inner join courses on students.stuid=scores.stuid and scores.courseid=courses.courseid order by score desc limit 5;
显示其成绩高于80的同学的名称及课程;
select name,course,score from students inner join scores inner join courses on students.stuid=scores.stuid and scores.courseid=courses.courseid where score>80;
# 另一种方法:
select name,course,score
from students,scores,courses
where students.stuid=scores.stuid and scores.courseid=courses.courseid and score>80;
求前8位同学每位同学自己两门课的平均成绩,并按降序排列
# 增加sum列的目的是过滤只学了一门课的同学
select stuid,avg(score),sum(score) from scores group by stuid having avg(score)<sum(score) order by avg(score) desc limit 8;
显示每门课程课程名称及学习了这门课的同学的个数
select course,sum(stuid) from students
inner join scores inner join courses on students.stuid=scores.stuid and scores.courseid=courses.courseid
group by course;
# 另一种
select course,sum(stuid) from scores inner join courses on scores.courseid=courses.courseid group by scores.courseid;
如何显示其年龄大于平均年龄的同学的名字
select name,age from students where age > (select avg(age) from students);
如何显示其学习的课程为第1、2,4或第7门课的同学的名字
select name,courseid from students inner join scores on students.stuid=scores.stuid where courseid in (1,2,4,7);
如何显示其成员数最少为3个的班级的同学中年龄大于同班同学平均年龄的同学
select * from students as a
inner join (select classid,count(stuid),avg(age) as aage from students where classid >0 group by classid having count(stuid)>=3 ) as b on a.classid=b.classid
where a.age>b.aage;
# 将复杂的查询分解为一个一个小的查询
select classid,count(classid),avg(age) from students group by classid having count(classid)>=3;
+---------+----------------+----------+
| classid | count(classid) | avg(age) |
+---------+----------------+----------+
| 1 | 4 | 20.5000 |
| 2 | 3 | 36.0000 |
| 3 | 5 | 32.2000 |
| 4 | 4 | 24.7500 |
| 6 | 4 | 20.7500 |
| 7 | 3 | 19.6667 |
+---------+----------------+----------+
select * from students where classid=1 and age>20.5;
select * from students where classid=2 and age>36.0;
统计各班级中年龄大于全校同学平均年龄的同学
select * from students where age > (select avg(age) from students );