sql 笔试题(四)

一道经典SQL面试题及答案

现在我们假设只有一个table,名为pages,有四个字段,id, url,title,body。里面储存了很多网页,网页的url地址,title和网页的内容,然后你用一个sql查询将url匹配的排在最前, title匹配的其次,body匹配最后,没有任何字段匹配的,不返回。

就是上面这道面试题,让我想了一个下午,在网上找资料,最后用下面方法实现


SELECT *
FROM page where url like '%baidu%' or title like '%baidu%' or like ''
ORDER BY CHARINDEX('baidu', url) DESC, CHARINDEX('baidu', title) DESC,
      CHARINDEX('baidu', body) DESC

但我感觉这种方法并不是最简单的,后来把这个方法发给面试的人,他给我了一种更简单方法,只要用基本的Sql语句就可以实现。代码如下

select a.[id],a.mark from
(
select [page].[id],100 as mark from [page] where [page].[url] like '%baidu%'
union
select [page].[id],50 as mark from [page] where [page].[title] like '%baidu%'
union
select [page].[id],10 as mark from [page] where [page].[body] like '%baidu%'
) as a  order by mark desc

用union 实现联合查询,在每个查询语句中定义一个临时变量mark 并给mark赋值,在最后的输出时采用mark来排序,这样实现,非常简单,我感觉这题更多考研我们的编程思想。

select url,title,body from pages where url<>'' or title<>'' or body <>''

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(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 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列否则选择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.在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
-------------------------------
一月份 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

 

1、删除表中重复的记录(记录完全相同才算重复)

select distinct * into #table_name from table_name
delete from table_name
select * into table_name from #table_name
drop table #table_name

2、学生表student,字段sID,sName,sAge;教师表teacher,字段tID,TName,tAge;教师-学生表ts,字段id,tID,sID。请统计每个教师有多少学生,要求教师年龄大于30,学生年龄大于12,并列出教师ID,教师姓名,学生人数答案:如果教师不同名,则语句如下

select teacher.tid,teacher.tname,count(*) as snum from ts,teacher,student
where teacher.tid=ts.tid and student.sid=ts.sid and teacher.tage>30 and student.sage>22
group by teacher.tid,teacher.tname

如果教师有同名,则语句如下

select temptable.tid,tname,temptable.snum from teacher,(select teacher.tid,count(*) as snum from ts,teacher,student where teacher.tid=ts.tid and student.sid=ts.sid and teacher.tage>30 and student.sage>22 group by teacher.tid) temptable
where teacher.tid=temptable.tid
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
涉及到了基础SQL的所有知识点 desc emp;描述一下emp表的结构 2 select from salgrade; 3 select ename sal 12 from emp; 4 select 2 3 from emp;会现14行 5 select 2 3 from dual;只有一行 专门提供计算数学表达式的 6 select sysdate from dual;获取当前系统时间 7 select ename sal 12 annual salary from emp;给sal 12取了个别名叫做annual salary 8 select ename sal 12 "annual salary" from emp;用双引号将别名括起来 这样可以保留住空格 大小写 中文等等 9 select ename comm from emp;其中comm中包括空值 空值不等于0 10 select ename sal 12+comm "总年薪" from emp;任何数加上空值都等于空值 11 select ename||sal from emp;字符串连接符 12 select ename||" nihao" from emp;在SQL语句中 字符串是由单引号括起来的若干字符 13 select ename||" niahao""dajiahao" from emp;如果字符串中本身就有一个单引号 那么在SQL语句中进行表示时 用两个单引号表示一个单引号 14 select distinct deptno from emp;select distinct deptno job from emp; 15 select from emp where ename "CLARK";where中添加条件约束 并且字符串需要加上单引号来表示 16 select ename job sal from emp where ename > "CBA";字符串比较 先比较第一个 比C大 就TRUE 第一个字母相同)否则比较下一个 比B大 就TRUE 否则 17 select ename sal from emp where sal<>ALL 1500 2000 2500 3000 ;<>表示不等于 <>ALL表示不等于所有的( ) 18 select from emp where sal between 800 and 1300;和select from emp where sal> 800 and sal< 1300;的效果是一样的 19 select from emp where comm is null;is null表示是空值 is not null表示非空值 不可以用 20 select ename sal from emp where ename in "SMITH" "CLARK" "SCOTT" ;ename在()中的人选来 ()中也可以是其他类型 21 select ename sal hiredate from emp where hiredate > "20 2月 81";日期的格式 22 select ename sal from emp where ename like " A%"; 表示一个字符 %表示0个或多个字符 23 select from emp where ename like "% %%" escape " ";其中 作为转义字符的符号 select from emp where ename like "% %%"; %表示百分号 24 select empno ename from emp order by empno asc;升序排列(默认);select deptno dname from dept order by deptno desc;降序排列 25 select empno ename from emp where deptno<>10 order by empno desc;可以先选择在排序 26 select ename sal deptno from emp order by deptno asc ename desc;先按照deptno进行升序排列 在deptno相同的地方 按照ename进行降序排列 27 select ename sal 12 annual sal from emp where ename not like " A%’ and sal > 800 order by desc; 28 select ename sal from emp where lower ename like " a%";小写 29 select substr ename 2 3 from emp;从第二个字符开始 截3个字符来作为结果 30 select chr 65 from dual;把ASCII码转化成字符 31 select ascii "A" from dual;把字符转化成ASCII码 32 select round 23 643 2 from dual;舍五入为23 64 默认第二个参数为0 即舍五入到各位 也可以为 1 则为20 33 select to char sal "$99 999 9999" from emp;美元 9代表一个数字 “ ”代表一个千分位符号 select to char sal "L99 999 9999" from emp;人民币 select to char sal "L00000 0000" from emp; 34 select to char hiredate "YYYY MM DD HH:MI:SS" from emp;转换Date类型的时间表示方法 select to char sysdate "MM DD YY HH24:MI:SS" from dual;24小时制 35 select ename hiredate from emp where hiredate > to Date "1981 2 3 14:23:12" "YYYY MM DD HH24:MI:SS" ;to date "" "" 将日期转换成自己想要的格式 36 select ename sal 12 + nvl comm 0 from emp;nvl comm 0 是将comm中的空值设为0 非空值还是本身 37 to number "$1250 00" "$9 999 99" ;有点问题 ">涉及到了基础SQL的所有知识点 desc emp;描述一下emp表的结构 2 select from salgrade; 3 select ename sal 12 from emp; 4 select 2 3 from emp;会现14行 5 select 2 3 from dual;只有一行 专门提供计算数学表达式的 6 select sysdate from dual;获 [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值