sql数据库面试题及答案【篇一:sql数据库经典面试题(笔试题)】=txt>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 22005-05-10 1 2------------------------------------------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,负)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 rq2) 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.rq3)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) bwhere a.col001=b.col0012.请教一个面试中遇到的sql语句的查询问题表中有a b c三列,用sql语句实现:当a列大于b列时选择a列否则选择b列,当b列大于c列时选择b列否则选择c列。
------------------------------------------select (case when ab then a else b end ),(case when bc then b esle c end)from table_name3.面试题:一个日期判断的sql语句?请取出tb_send表中日期(sendtime字段)为当天的所有记录?(sendtime字段为datetime型,包含日期与时间)------------------------------------------select * from tb where datediff(dd,sendtime,getdate())=04.有一张表,里面有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 table5.在sqlserver2000中请用sql创建一张用户临时表和系统临时表,里面包含两个字段id和idvalues,类型都是int型,并解释下两者的区别?------------------------------------------用户临时表:create table #xx(id int, idvalues int)系统临时表:create table ##xx(id int, idvalues int)区别:用户临时表只对创建这个表的用户的session可见,对其他进程是不可见的. 当创建它的进程消失时这个临时表就自动删除.全局临时表对整个sql server实例都可见,但是所有访问它的session都消失的时候,它也自动删除.6.sqlserver2000是一种大型数据库,他的存储容量只受存储介质的限制,请问它是通过什么方式实现这种无限容量机制的。
------------------------------------------它的所有数据都存储在数据文件中(*.dbf),所以只要文件够大,sql server的存储容量是可以扩大的.sql server 2000 数据库有三种类型的文件:主要数据文件主要数据文件是数据库的起点,指向数据库中文件的其它部分。
每个数据库都有一个主要数据文件。
主要数据文件的推荐文件扩展名是 .mdf。
次要数据文件次要数据文件包含除主要数据文件外的所有数据文件。
有些数据库可能没有次要数据文件,而有些数据库则有多个次要数据文件。
次要数据文件的推荐文件扩展名是 .ndf。
日志文件日志文件包含恢复数据库所需的所有日志信息。
每个数据库必须至少有一个日志文件,但可以不止一个。
日志文件的推荐文件扩展名是 .ldf。
7.请用一个sql语句得出结果从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。
如使用存储过程也可以。
table1月份mon 部门dep 业绩yj-------------------------------一月份0110一月份0210一月份035二月份028二月份049三月份038table2部门dep部门名称dname--------------------------------01国内业务一部02国内业务二部03国内业务三部04国际业务部table3 (result)【篇二:sql数据库试题(笔试题)】部门,平均工资,要求按部门的字符串顺序排序,不能含有human resource部门,employee结构如下:employee_id, employee_name,depart_id,depart_name,wage答:select depart_name, avg(wage)from employeewhere depart_name human resourcegroup by depart_nameorder by depart_name-------------------------------------------------------------------------- 29.给定如下sql数据库:test(num int(4)) 请用一条sql语句返回num的最小值,但不许使用统计功能,如min,max等答:select top 1 numfrom testorder by num--------------------------------------------------------------------------33.一个数据库中有两个表:一张表为customer,含字段id,name;一张表为order,含字段id,customerid(连向customer中id的外键),revenue;写出求每个customer的revenue总和的sql语句。
建表 create table customer(id int primary key,name char(10))gocreate table [order](id int primary key,customerid int foreign key references customer(id) , revenue float)go--查询select customer.id, sum( isnull([order].revenue,0) )from customer full join [order]on( [order].customerid=customer.id )group by customer.idselect customer.id,sum(order.revener) from order,customer where customer.id=customerid group by customer.idselect customer.id, sum(order.revener )from customer full join orderon( order.customerid=customer.id )group by customer.id5数据库(10)a tabel called “performance”contain :name and score,please 用sql语言表述如何选出score最high的一个(仅有一个)仅选出分数,select max(score) from performance仅选出名字,即选出名字,又选出分数:select top 1score ,name from per order by scoreselect name1,score from per where score in/=(selectmax(score) from per).....4 有关系 s(sno,sname) c(cno,cname) sc(sno,cno,grade)1 问上课程 db的学生noselect count(*) from c,sc where ame=db and o=o select count(*) from sc where cno=(select cno from c whereame=db)2 成绩最高的学生号select sno from sc where grade=(select max(grade) from sc ) 3 每科大于90分的人数select ame,count(*) from c,sc where o=o andsc.grade90 group by ameselect ame,count(*) from c join sc on o=o and sc.grade90group by ame数据库笔试题*建表:dept:deptno(primary key),dname,locemp:empno(primary key),ename,job,mgr,sal,deptno*/1 列出emp表中各部门的部门号,最高工资,最低工资select max(sal) as 最高工资,min(sal) as 最低工资,deptno from emp group by deptno;2 列出emp表中各部门job为clerk的员工的最低工资,最高工资 select max(sal) as 最高工资,min(sal) as 最低工资,deptno as 部门号 from emp where job = clerk group by deptno;3 对于emp中最低工资小于1000的部门,列出job为clerk的员工的部门号,最低工资,最高工资select deptno,max(),min(sal) from emp as b where job=”clerk” and 1000(select min(sal)from emp as a where a.deptno=b.deptno)group by b.deptnoselect max(sal) as 最高工资,min(sal) as 最低工资,deptno as 部门号 from emp as bwhere job=clerk and 1000(select min(sal) from emp as a wherea.deptno=b.deptno) group by b.deptno4 根据部门号由高而低,工资有低而高列出每个员工的姓名,部门号,工资select deptno as 部门号,ename as 姓名,sal as 工资 from emp order by deptno desc,sal asc5 写出对上题的另一解决方法(请补充)6 列出张三所在部门中每个员工的姓名与部门号select ename,deptno from emp where deptno = (select deptno from emp where ename = 张三)7 列出每个员工的姓名,工作,部门号,部门名select ename,job,emp.deptno,dept.dname from emp,deptwhere emp.deptno=dept.deptno8 列出emp中工作为clerk的员工的姓名,工作,部门号,部门名select ename,job,dept.deptno,dname from emp,dept wheredept.deptno=emp.deptno and job=clerk9 对于emp中有管理者的员工,列出姓名,管理者姓名(管理者外键为mgr) select a.ename as 姓名,b.ename as 管理者 from emp as a,emp as b where a.mgr is not null and a.mgr=b.empno10 对于dept表中,列出所有部门名,部门号,同时列出各部门工作为clerk的员工名与工作select dname as 部门名,dept.deptno as 部门号,ename as 员工名,job as 工作 from dept,empwhere dept.deptno *= emp.deptno and job = clerk11 对于工资高于本部门平均水平的员工,列出部门号,姓名,工资,按部门号排序select a.deptno as 部门号,a.ename as 姓名,a.sal as 工资 from emp as a where a.sal(select avg(sal) from emp as b wherea.deptno=b.deptno) order by a.deptno12 对于emp,列出各个部门中平均工资高于本部门平均水平的员工数和部门号,按部门号排序select count(a.sal) as 员工数,a.deptno as 部门号 from emp as a where a.sal(select avg(sal) from emp as b wherea.deptno=b.deptno) group by a.deptno order by a.deptno13 对于emp中工资高于本部门平均水平,人数多与1人的,列出部门号,人数,按部门号排序select count(a.empno) as 员工数,a.deptno as 部门号,avg(sal)as 平均工资 from emp as awhere (select count(c.empno) from emp as c wherec.deptno=a.deptno and c.sal(select avg(sal) from emp as b where c.deptno=b.deptno))1 group by a.deptno order bya.deptno14 对于emp中低于自己工资至少5人的员工,列出其部门号,姓名,工资,以及工资少于自己的人数select a.deptno,a.ename,a.sal,(select count(b.ename) fromemp as b where b.sala.sal) as 人数 from emp as awhere (select count(b.ename) from emp as b whereb.sala.sal)5 数据库笔试题及答案第一套一.选择题1. 下面叙述正确的是ccbad ______。