SQL面试题

更新时间:2023-12-31 05:27:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

SQL经典面试题及答案

2007年07月27日星期五上午 08:42 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语句?

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

------------------------------------------ create table #tmp(rqvarchar(10),shengfunchar(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','负')

1)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 2) 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 3)select a.col001,a.a1 胜,b.b1 负 from

(select col001,count(col001) a1 from temp1 where col002='胜' group by col001) a, (select col001,count(col001) b1 from temp1 where col002='负' group by col001) b where a.col001=b.col001

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

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

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.在sqlserver2000中请用sql创建一张用户临时表和系统临时表,里面包含两个字段ID和IDValues, 类型都是int型,并解释下两者的区别?

------------------------------------------ 用户临时表:create table #xx(ID int, IDValuesint) 系统临时表:create table ##xx(ID int, IDValuesint) 区别:

用户临时表只对创建这个表的用户的Session可见,对其他进程是不可见的. 当创建它的进程消失时这个临时表就自动删除.

全局临时表对整个SQL Server实例都可见,但是所有访问它的Session都消失的时候,它也自动删除.

6.sqlserver2000是一种大型数据库,他的存储容量只受存储介质的限制,请问它是通过什么方式实现这种无限容量机------------------------------------------

它的所有数据都存储在数据文件中(*.dbf),所以只要文件够大,SQL Server的存储容量是可以扩大的.

SQL Server 2000 数据库有三种类型的文件:

主要数据文件

主要数据文件是数据库的起点,指向数据库中文件的其它部分。每个数据库都有一个主要数据文件。 主要数据文件的推荐文件扩展名是 .mdf。

次要数据文件

次要数据文件包含除主要数据文件外的所有数据文件。有些数据库可能没有次要数据文件, 而有些数据库则有多个次要数据文件。次要数据文件的推荐文件扩展名是 .ndf。 日志文件

日志文件包含恢复数据库所需的所有日志信息。每个数据库必须至少有一个日志文件,但可以不止一个。 日志文件的推荐文件扩展名是 .ldf。 7.请用一个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

------------------------------------------ 1)

select a.部门名称dname,b.业绩yj as '一月份',c.业绩yj as '二月份',d.业绩yj as '三月份' from table1 a,table2 b,table2 c,table2 d

where a.部门dep = b.部门dep and b.月份mon = '一月份' and a.部门dep = c.部门dep and c.月份mon = '二月份' and a.部门dep = d.部门dep and d.月份mon = '三月份' and 2)

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

表形式如下: 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

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)

2.学生表 如下:

自动编号 学号 姓名课程编号课程名称分数

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

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

一个叫department的表,里面只有一个字段name,一共有4条纪录,

分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.

你先按你自己的想法做一下,看结果有我的这个简单吗? 答:select a.name, b.name from team a, team b where a.name < b.name

请用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 ************************************************************************************ 面试题:怎么把这样一个表儿 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 这个是Oracle 中做的:

select * from (select name, year b1, lead(year) over

(partition by name order by year) b2, lead(m,2) over(partition by name order by year) b3,rank()over( partition by name order by year) rk from t) where rk=1;

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people

where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录 delete from people

where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1) 3、查找表中多余的重复记录(多个字段) select * from vitae a

where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录 delete from vitae a

where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1) 5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录 select * from vitae a

where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1) (二)

比方说

在A表中存在一个字段“name”,

而且不同记录之间的“name”值有可能会相同,

现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项; Select Name,Count(*) From A Group By Name Having Count(*) > 1 如果还查性别也相同大则如下:

Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1 (三) 方法一

declare @max integer,@id integer

declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1 open cur_rows

fetch cur_rows into @id,@max while @@fetch_status=0 begin

select @max = @max -1 set rowcount @max

delete from 表名 where 主字段 = @id fetch cur_rows into @id,@max end

close cur_rows set rowcount 0 方法二

有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。 1、对于第一种重复,比较容易解决,使用 select distinct * from tableName 就可以得到无重复记录的结果集。

如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除 select distinct * into #Tmp from tableName drop table tableName

select * into tableName from #Tmp drop table #Tmp

发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。 2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下 假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集 select identity(int,1,1) as autoID, * into #Tmp from tableName

select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID select * from #Tmp where autoID in(select autoID from #tmp2)

最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列) (四)查询重复

select * from tablename where id in ( select id from tablename group by id

having count(id) > 1 )

本文来源:https://www.bwwdw.com/article/abmx.html

Top