sql经典面试题——oracle

1、 用一条SQL 语句 查询出每门课都大于80 分的学生姓名
name kecheng fenshu
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90

A: 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

2、学生表 如下:

自动编号 学号 姓名 课程编号 课程名称 分数
1 2005001 张三 0001 数学 69
2 2005002 李四 0001 数学 89
3 2005001 张三 0001 数学 69
删除除了自动编号不同, 其他都相同的学生冗余信息

A: delete table where 自动编号 not in(select min( 自动编号) from table group by 学号, 姓名, 课程编号, 课程名称, 分数)

 

3、 面试题:怎么把这样一个表儿

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

答案一、

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

 

5、有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):
大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。
显示格式:
语文 数学 英语
及格 优秀 不及格  

--------------------------------------------

SELECT (CASE WHEN 语文 >=80 THEN '优秀' 
             WHEN 语文 >=60 THEN '及格'
             else '不及格'
        END) AS 语文 ,
        (CASE WHEN 数学 >=80 THEN '优秀' 
             WHEN 数学 >=60 THEN '及格'
             else '不及格'
        END) AS 数学 ,
        (CASE WHEN 英语 >=80 THEN '优秀' 
             WHEN 英语 >=60 THEN '及格'
             else '不及格'
        END) AS 英语 
from TABLE;

 

 6、一道SQL语句面试题,关于group by

表内容:
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负

如果要生成下列结果, 该如何写sql语句?

            胜 负
2005-05-09 2 2
2005-05-10 1 2

---------------------------------------------------------

答案1

select year,
(select count(losu) from taba a where a.losu='' and a.year=taba.year ) as 胜,
(select count(losu) from taba a where a.losu='' and a.year=taba.year ) asfrom taba group by year;

答案2

select year,
sum(case when losu='' then 1 else 0 end ) as 胜,
sum(case when losu='' then 1 else 0 end ) asfrom taba group by year ;

 

7、表形式如下:
Year Salary
2000 1000
2001 2000
2002 3000
2003 4000

想得到如下形式的查询结果
Year Salary
2000 1000
2001 3000
2002 6000
2003 10000

sql语句怎么写?

select b.year, sum(a.salary) from hello a, hello b where a.year <= b.year group by b.year;

 

8、一个叫teamt的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.

select a.name, b.name 
from team a, team b 
where a.name < b.name

 

9、请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101科目相应月份的发生额高的科目。请注意:TestDB中有很多科目,都有1-12月份的发生额。
AccID:科目代码,Occmonth:发生额月份,DebitOccur:发生额。
数据库名:JcyAudit,数据集:Select * from TestDB

答:select a.*
from TestDB a
,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b
where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur

 

10、原表:
courseid coursename score
-------------------------------------
1 java 70
2 oracle 90
3 xml 40
4 jsp 30
5 servlet 80
-------------------------------------
为了便于阅读,查询此表后的结果显式如下(及格分数为60):
courseid coursename score mark
---------------------------------------------------
1 java 70 pass
2 oracle 90 pass
3 xml 40 fail
4 jsp 30 fail
5 servlet 80 pass 

select courseid,coursename,score,(case when score >=60 then 'pass' else 'fail' end) mark from sc
select courseid,coursename,score,(decode(sign(score-60),-1,'fail','pass') mark from sc

 

11.原表
id proid proname
1 1 M
1 2 F
2 1 N
2 2 G
3 1 B
3 2 A
查询后的表
id pro1 pro2
1 M F
2 N G
3 B A

select a.id,
(select b.proname from table3 b where proid=1 and b.id=a.id) pro1,
(select b.proname from table3 b where proid=2 and b.id=a.id) pro2
from table3 a
group by id;

 

12、sql求解
a
a1 a2
记录 1 a
1 b
2 x
2 y
2 z
select能选成以下结果吗?
1 ab
2 xyz

 

posted @ 2018-06-05 14:20  茹此这样  阅读(3076)  评论(1编辑  收藏  举报