到处淘来的SQL题
1. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名
name kecheng fenshu
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90
2. 面试题:怎么把这样一个表儿
year month amount
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
查成这样一个结果
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
答案:
1.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select distinct name from table where name not in (select distinct name from table where fenshu<=80)
select name from table group by name having min(fenshu)>80
还可以:
select name from biao group by name having fenshu>80
2.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select year, (select amount from aaa m where month=1 and m.year=aaa.year) as m1, (select amount from aaa m where month=2 and m.year=aaa.year) as m2, (select amount from aaa m where month=3 and m.year=aaa.year) as m3, (select amount from aaa m where month=4 and m.year=aaa.year) as m4 from aaa group by year
3.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):
大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。
显示格式:
语文 数学 英语
及格 优秀 不及格
答案:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select (case when 语文>=80 then '优秀' when 语文>=60 then '及格' else '不及格') as 语文, (case when 数学>=80 then '优秀' when 数学>=60 then '及格' else '不及格') as 数学, (case when 英语>=80 then '优秀' when 英语>=60 then '及格' else '不及格') as 英语, from table
4.一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。
答案:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
select id, Count(*) from tb group by id having count(*)>1
select * from(select count(ID) as count from table group by ID)T where T.count>1
做个快乐的自己。