您的位置:首页 > 职场人生

一道关于sql的面试题

2016-09-10 10:13 441 查看
存在下面的数据,请通过一条sql语句将其中每门课的分数都大于80的筛选出来

names  classes  score

A        语文    83

A        数学    87

B        语文    83

B        数学    67

C        语文    83

C        数学    87

C        英语    77

insert into  ttt values('A','语文',83);

insert into  ttt values('A','数学',87);

insert into  ttt values('B','语文',83);

insert into  ttt values('B','数学',67);

insert into  ttt values('C','语文',83);

insert into  ttt values('C','数学',83);

insert into  ttt values('C','英语',77);

1.原理: 将大于80的个数找出来 与总的比较。 然后筛选出相等的即可

select a.name1 from 

(select count(t1.classes) num1, t1.names name1 from ttt t1 where t1.score > 80 group by t1.names) a,

(select count(t2.classes) num2, t2.names name2 from ttt t2  group by t2.names) b

 where a.num1 = b.num2 and a.name1 = b.name2;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: