ITPub博客

首页 > > > SQL面试题-代码篇二

SQL面试题-代码篇二

作者:liyue071714118 时间:2018-05-11 16:06:25 0 删除 编辑

(点击上方蓝色字,可快速关注我们哦)


1.一道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语句?

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

ps:当年面试就遇到这个题


create table tmp(rq varchar(10),shengfu nchar(1));

insert into tmp values('2005-05-09','胜');
insert into tmp values('2005-05-09','胜');
insert into tmp values('2005-05-09','负');
insert into tmp values('2005-05-09','负');
insert into tmp values('2005-05-10','胜');
insert into tmp values('2005-05-10','负');
insert into tmp values('2005-05-10','负');

--方法一
select rq,
sum(case when shengfu='胜' then 1 else 0 end)'胜',
sum(case when shengfu='负' then 1 else 0 end)'负'
from tmp group by rq;
--方法二
select N.rq,N.勝,M.負
from (
select rq,count(*) from tmp where shengfu='胜' group by rq) N
inner join
(
select rq,count(*) from tmp where shengfu='负' group by rq) M
on N.rq=M.rq;


2.请教一个面试中遇到的SQL语句的查询问题

表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。


select 
(case when a>b then a else b end ),
(case when b>c then b esle c end)
from table_name


3.面试题:一个日期判断的sql语句?

请取出tb_send表中日期(SendTime字段)为当天的所有记录?(SendTime字段为datetime型,包含日期与时间)


select * from tb 
where datediff(dd,SendTime,getdate())=0


4.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):  

大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。  

显示格式:  
语文              数学                英语  
及格              优秀                不及格    

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


5.请用一个sql语句得出结果
从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。
如使用存储过程也可以。
table1

月份mon 部门dep 业绩yj
一月份 01 10
一月份 02 10
一月份 03 5
二月份 02 8
二月份 04 9
二月份 03 8

table2

部门dep 部门名称dname
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国内业务部

table3 (result)

部门dep 一月份 一月份 一月份
01 10 null null
02 10 8 null
03 null 5 8
04 null null 9

select
a.dep,
sum(case when b.mon=1 then b.yj else 0 end) as '一月份',
sum(case when b.mon=2 then b.yj else 0 end) as '二月份',
sum(case when b.mon=3 then b.yj else 0 end) as '三月份',
sum(case when b.mon=4 then b.yj else 0 end) as '四月份',
sum(case when b.mon=5 then b.yj else 0 end) as '五月份',
sum(case when b.mon=6 then b.yj else 0 end) as '六月份',
sum(case when b.mon=7 then b.yj else 0 end) as '七月份',
sum(case when b.mon=8 then b.yj else 0 end) as '八月份',
sum(case when b.mon=9 then b.yj else 0 end) as '九月份',
sum(case when b.mon=10 then b.yj else 0 end) as '十月份',
sum(case when b.mon=11 then b.yj else 0 end) as '十一月份',
sum(case when b.mon=12 then b.yj else 0 end) as '十二月份',
from table2 a left join table1 b on a.dep=b.dep


8.一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。

--方法一
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


9.表形式如下:

 

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) salary
FROM hello a, hello b
WHERE a.YEAR <= b.YEAR GROUP BY b.YEAR

--子查询
select year ,
(select sum(salary)
from hello as B
where B.year<=A.year )
from hello as A


长按识别二维码,
更多咨询早知道!


原文作者:;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2021-12-10

  • 博文量
    1486
  • 访问量
    11425