一道關於sql的面試題

存在下面的數據,請通過一條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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章