百度一面时的数据库查询的题:
name | cource | score |
张三 | 语文 | 81 |
张三 | 数学 | 75 |
李四 | 语文 | 76 |
李四 | 数学 | 90 |
王五 | 语文 | 81 |
王五 | 数学 | 100 |
王五 | 英语 | 90 |
用一条SQL语句查询出所有科目都大于80分的学生名单:
select distinct name from table where are are not in (select distinct name from table where score<=80);
select distinct name from table group by name having min(score)>80;
查询平均分大于80分的学生名单:
select distinct name from table group by name having avg(score)>80;
select distinct name, avg(score) as sc from table group by name having avg(score)>80;//将平均分属性添加到表中