欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

总结了一些sql的面试题及答案供参考

程序员文章站 2022-03-09 21:26:14
总结了一些sql的面试题及答案供参考 1.用一条sql语句 查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三 语文81 张三 数学75 李四 语文76 李四 数学90...

总结了一些sql的面试题及答案供参考

1.用一条sql语句 查询出每门课都大于80分的学生姓名

name kecheng fenshu

张三 语文81

张三 数学75

李四 语文76

李四 数学90

王五 语文81

王五 数学100

王五 英语90

a: select distinct name from table where name not in (selectdistinct 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 * fromtestdb

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

************************************************************************************

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

yearmonth 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 m1m2m3m4

1991 1.1 1.2 1.3 1.4

1992 2.1 2.2 2.3 2.4

答案一、

select year,

(select amount fromaaa m where month=1andm.year=aaa.year) as m1,

(select amount fromaaa m where month=2andm.year=aaa.year) as m2,

(select amount fromaaa m where month=3andm.year=aaa.year) as m3,

(select amount fromaaa m where month=4andm.year=aaa.year) as m4

from aaagroup 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 byyear) b3,rank()over(

partition by name order by year) rk from t) where rk=1;

************************************************************************************

精妙的sql语句!

精妙sql语句

作者:不详 发文时间:2003.05.29 10:55:05

说明:复制表(只复制结构,源表名:a 新表名:b)

sql: select * into b from a where 1<>1

说明:拷贝表(拷贝数据,源表名:a 目标表名:b)

sql: insert into b(a, b, c) select d,e,f from b;

说明:显示文章、提交人和最后回复时间

sql: select a.title,a.username,b.adddate from table a,(select max(adddate)adddate from table where table.title=a.title) b

说明:外连接查询(表名1:a 表名2:b)

sql: select a.a, a.b, a.c, b.c, b.d, b.f from a left out join b on a.a = b.c

说明:日程安排提前五分钟提醒

sql: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5

说明:两张关联表,删除主表中已经在副表中没有的信息

sql:

delete from info where not exists ( select * from infobz whereinfo.infid=infobz.infid )

说明:--

sql:

select a.num, a.name, b.upd_date, b.prev_upd_date

from table1,

(select x.num, x.upd_date, y.upd_date prev_upd_date

from (select num, upd_date, inbound_qty, stock_onhand

from table2

where to_char(upd_date,'yyyy/mm') = to_char(sysdate, 'yyyy/mm')) x,

(select num, upd_date, stock_onhand

from table2

where to_char(upd_date,'yyyy/mm') =

to_char(to_date(to_char(sysdate, 'yyyy/mm') || '/01','yyyy/mm/dd') - 1,'yyyy/mm') ) y,

where x.num = y.num (+)

and x.inbound_qty + nvl(y.stock_onhand,0) <> x.stock_onhand ) b

where a.num = b.num

说明:--

sql:

select * from studentinfo where not exists(select * from student wherestudentinfo.id=student.id) and 系名称='"&strdepartmentname&"'and 专业名称='"&strprofessionname&"'order by 性别,生源地,高考总成绩

说明:

从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源)

sql:

select a.userper, a.tel, a.standfee, to_char(a.telfeedate, 'yyyy') as telyear,

sum(decode(to_char(a.telfeedate, 'mm'), '01', a.factration)) as jan,

sum(decode(to_char(a.telfeedate, 'mm'), '02', a.factration)) as fri,

sum(decode(to_char(a.telfeedate, 'mm'), '03', a.factration)) as mar,

sum(decode(to_char(a.telfeedate, 'mm'), '04', a.factration)) as apr,

sum(decode(to_char(a.telfeedate, 'mm'), '05', a.factration)) as may,

sum(decode(to_char(a.telfeedate, 'mm'), '06', a.factration)) as jue,

sum(decode(to_char(a.telfeedate, 'mm'), '07', a.factration)) as jul,

sum(decode(to_char(a.telfeedate, 'mm'), '08', a.factration)) as agu,

sum(decode(to_char(a.telfeedate, 'mm'), '09', a.factration)) as sep,

sum(decode(to_char(a.telfeedate, 'mm'), '10', a.factration)) as oct,

sum(decode(to_char(a.telfeedate, 'mm'), '11', a.factration)) as nov,

sum(decode(to_char(a.telfeedate, 'mm'), '12', a.factration)) as dec

from (select a.userper, a.tel, a.standfee, b.telfeedate, b.factration

from telfeestand a, telfee b

where a.tel = b.telfax) a

group by a.userper, a.tel, a.standfee, to_char(a.telfeedate, 'yyyy')

说明:四表联查问题:

sql: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.cinner join d on a.a=d.d where .....

说明:得到表中最小的未使用的id号

sql:

select (case when exists(select * from handle b where b.handleid = 1) thenmin(handleid) + 1 else 1 end) as handleid

from handle

where not handleid in (select a.handleid - 1 from handle a)

*******************************************************************************

有两个表a和b,均有key和value两个字段,如果b的key在a中也有,就把b的value换为a中对应的value

这道题的sql语句怎么写?

update b set b.value=(select a.value from a where a.key=b.key) where b.id in(select b.id from b,a where b.key=a.key);

***************************************************************************

高级sql面试题

原表:

courseid coursename score

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

1 java 70

2 oracle 90

3 xml 40

4 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

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

写出此查询语句

没有装oracle,没试过

select courseid, coursename ,score ,decode(sign(score-60),-1,'fail','pass') as mark from course

完全正确

sql> desc course_v

name null type

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

courseid number

coursename varchar2(10)

score number

sql> select * from course_v;

courseid coursename score

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

1 java 70

2 oracle 90

3 xml 40

4 jsp 30

5 servlet 80

sql> select courseid, coursename ,score,decode(sign(score-60),-1,'fail','pass') as mark from course_v;

courseid coursename score mark

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

1 java 70 pass

2 oracle 90 pass

3 xml 40 fail

4 jsp 30 fail

5 servlet 80 pass

*******************************************************************************

原表:

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

写出查询语句

解决方案

sql求解

表a

列 a1 a2

记录 1 a

1 b

2 x

2 y

2 z

用select能选成以下结果吗?

1 ab

2 xyz

使用pl/sql代码实现,但要求你组合后的长度不能超出oracle varchar2长度的限制。

下面是一个例子

create or replace type strings_table is table ofvarchar2(20);

/

create or replace function merge (pv in strings_table)return varchar2

is

ls varchar2(4000);

begin

for i in 1..pv.count loop

ls := ls || pv(i);

end loop;

return ls;

end;

/

create table t (id number,name varchar2(10));

insert into t values(1,'joan');

insert into t values(1,'jack');

insert into t values(1,'tom');

insert into t values(2,'rose');

insert into t values(2,'jenny');

column names format a80;

select t0.id,merge(cast(multiset(select name from twhere t.id = t0.id) as strings_table)) names

from (select distinct id from t) t0;

drop type strings_table;

drop function merge;

drop table t;

用sql:

well if you have a thoretical maximum, which i wouldassume you would given the legibility of listing hundreds of employees in theway you describe then yes. but the sql needs to use the lag function for eachemployee, hence a hundred emps a hundred lags, so kind of bulky.

this example uses a max of 6, and would need more cut npasting to do more than that.

sql> select deptno, dname, emps

2 from (

3 select d.deptno, d.dname, rtrim(e.ename ||', '||

4 lead(e.ename,1) over (partition by d.deptno

5 order by e.ename) ||', '||

6 lead(e.ename,2) over (partition by d.deptno

7 order by e.ename) ||', '||

8 lead(e.ename,3) over (partition by d.deptno

9 order by e.ename) ||', '||

10 lead(e.ename,4) over (partition by d.deptno

11 order by e.ename) ||', '||

12 lead(e.ename,5) over (partition by d.deptno

13 order by e.ename),', ') emps,

14 row_number () over (partition by d.deptno

15 order by e.ename) x

16 from emp e, dept d

17 where d.deptno = e.deptno

18 )

19 where x = 1

20 /

deptno dname emps

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

10 accounting clark, king, miller

20 research adams, ford, jones, rooney, scott, smith

30 sales allen, blake, james, martin, turner, ward

also

先create function get_a2;

create or replace function get_a2( tmp_a1 number)

return varchar2

is

col_a2 varchar2(4000);

begin

col_a2:='';

for cur in (select a2 from unite_a where a1=tmp_a1)

loop

col_a2=col_a2||cur.a2;

end loop;

return col_a2;

end get_a2;

select distinct a1 ,get_a2(a1) from unite_a

1 abc

2 efg

3 kmn

*******************************************************************************

一个sql 面试题

去年应聘一个职位未果,其间被考了一个看似简单的题,但我没有找到好的大案.

不知各位大虾有无好的解法

题为:

有两个表, t1, t2,

table t1:

seller | non_seller

----- -----

a b

a c

a d

b a

b c

b d

c a

c b

c d

d a

d b

d c

table t2:

seller | coupon | bal

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

a 9 100

b 9 200

c 9 300

d 9 400

a 9.5 100

b 9.5 20

a 10 80

要求用select 语句列出如下结果:------如a的sum(bal)为b,c,d的和,b的sum(bal)为a,c,d的和.......

且用的方法不要增加数据库负担,如用临时表等.

non-seller| coupon | sum(bal) ------- --------

a 9 900

b 9 800

c 9 700

d 9 600

a 9.5 20

b 9.5 100

c 9.5 120

d 9.5 120

a 10 0

b 10 80

c 10 80

d 10 80

关于上那个sql微软面试题

问题:

一百个账户各有100$,某个账户某天如有支出则添加一条新记录,记录其余额。一百天后,请输出每天所有账户的余额信息

这个问题的难点在于每个用户在某天可能有多条纪录,也可能一条纪录也没有(不包括第一天)

返回的记录集是一个100天*100个用户的纪录集

下面是我的思路:

1.创建表并插入测试数据:我们要求username从1-100

create table [dbo].[table2] (

[username] [varchar] (50) not null , --用户名

[outdate] [datetime] not null , --日期

[cash] [float] not null --余额

) on [primary

declare @i int

set @i=1

while @i<=100

begin

insert table2 values(convert(varchar(50),@i),'2001-10-1',100)

insert table2 values(convert(varchar(50),@i),'2001-11-1',50)

set @i=@i+1

end

insert table2 values(convert(varchar(50),@i),'2001-10-1',90)

select * from table2 order by outdate,convert(int,username)

2.组合查询语句:

a.我们必须返回一个从第一天开始到100天的纪录集:

如:2001-10-1(这个日期是任意的)到 2002-1-8

由于第一天是任意一天,所以我们需要下面的sql语句:

select top 100 dateadd(d,convert(int,username)-1,min(outdate)) as outdate

from table2

group by username

order by convert(int,username)

这里的奥妙在于:

convert(int,username)-1(记得我们指定用户名从1-100 :-))

group by username,min(outdate):第一天就可能每个用户有多个纪录。

返回的结果:

outdate

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

2001-10-01 00:00:00.000

.........

2002-01-08 00:00:00.000

b.返回一个所有用户名的纪录集:

select distinct username from table2

返回结果:

username

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

1

10

100

......

99

c.返回一个100天记录集和100个用户记录集的笛卡尔集合:

select * from

(

select top 100 dateadd(d,convert(int,username)-1,min(outdate)) as outdate

from table2

group by username

order by convert(int,username)

) as a

cross join

(

select distinct username from table2

) as b

order by outdate,convert(int,username)

返回结果100*100条纪录:

outdate username

2001-10-01 00:00:00.000 1

......

2002-01-08 00:00:00.000 100

d.返回当前所有用户在数据库的有的纪录:

select outdate,username,min(cash) as cash from table2

group by outdate,username

order by outdate,convert(int,username)

返回纪录:

outdate username cash

2001-10-01 00:00:00.000 1 90

......

2002-01-08 00:00:00.000 100 50

e.将c中返回的笛卡尔集和d中返回的纪录做left join:

select c.outdate,c.username,

d.cash

from

(

select * from

(

select top 100 dateadd(d,convert(int,username)-1,min(outdate)) as outdate

from table2

group by username

order by convert(int,username)

) as a

cross join

(

select distinct username from table2

) as b

) as c

left join

(

select outdate,username,min(cash) as cash from table2

group by outdate,username

) as d

on(c.username=d.username and datediff(d,c.outdate,d.outdate)=0)

order by c.outdate,convert(int,c.username)

注意:用户在当天如果没有纪录,cash字段返回null,否则cash返回每个用户当天的余额

outdate username cash

2001-10-01 00:00:00.000 1 90

2001-10-01 00:00:00.000 2 100

......

2001-10-02 00:00:00.000 1 90

2001-10-02 00:00:00.000 2 null <--注意这里

......

2002-01-08 00:00:00.000 100 50

f.好了,现在我们最后要做的就是,如果cash为null,我们要返回小于当前纪录日期的第一个用户余额(由于我们使用order by cash,所以返回top 1纪录即可,使用min应该也可以),这个余额即为当前的余额:

case isnull(d.cash,0)

when 0 then

(

select top 1 cash from table2 where table2.username=c.username

and datediff(d,c.outdate,table2.outdate)<0

order by table2.cash

)

else d.cash

end as cash

g.最后组合的完整语句就是

select c.outdate,c.username,

case isnull(d.cash,0)

when 0 then

(

select top 1 cash from table2 where table2.username=c.username

and datediff(d,c.outdate,table2.outdate)<0

order by table2.cash

)

else d.cash

end as cash

from

(

select * from

(

select top 100 dateadd(d,convert(int,username)-1,min(outdate)) as outdate

from table2

group by username

order by convert(int,username)

) as a

cross join

(

select distinct username from table2

) as b

) as c

left join

(

select outdate,username,min(cash) as cash from table2

group by outdate,username

) as d

on(c.username=d.username and datediff(d,c.outdate,d.outdate)=0)

order by c.outdate,convert(int,c.username)

返回结果:

outdate username cash

2001-10-01 00:00:00.000 1 90

2001-10-01 00:00:00.000 2 100

......

2002-01-08 00:00:00.00010050

***********************************************************************************

取出sql表中第31到40的记录(以自动增长id为主键)

*从数据表中取出第n条到第m条的记录*/

declare @m int

declare @n int

declare @sql varchar(800)

set @m=40

set @n=31

set @sql='select top '+str(@m-@n+1) + '* from idetail where autoid not in(

select top '+ str(@n-1) + 'autoid from idetail)'

exec(@sql)

select top 10 * from t where id not in (select top 30 id from torder by id ) orde by id

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

select top 10 * from t where id in (select top 40 id from torder by id) order by id desc

*******************************************************************************

一道面试题,写sql语句

有表a存储二叉树的节点,要用一条sql语句查出所有节点及节点所在的层.

表a

c1 c2 a ----------1

---- ---- / \

a b b c --------2

a c / / \

b d d n e ------3

c e / \ \

d f f k i ---4

e i

d k

c n

所要得到的结果如下

jd cs

----- ----

a 1

b 2

c 2

d 3

n 3

e 3

f 4

k 4

i 4

有高手指导一下,我只能用pl/sql写出来,请教用一条sql语句的写法

sql> select c2, level + 1 lv

2 from test start

3 with c1 = 'a'

4 connect by c1 = prior c2

5 union

6 select 'a', 1 from dual

7 order by lv;

c2 lv

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

a 1

b 2

c 2

d 3

e 3

n 3

f 4

i 4

k 4

已选择9行。