求sql server 面试题

cwdhubin 2006-07-03 03:53:00
sql server 面试题,谁有请发我一份,谢谢
...全文
358 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cwdhubin 2006-07-06
  • 打赏
  • 举报
回复
结贴,谢谢楼上个各位!
huaxiangniaoyu 2006-07-04
  • 打赏
  • 举报
回复
楼上的这题要命。
bufan2162 2006-07-03
  • 打赏
  • 举报
回复
这种题目网上多了去了
附加一题:

请问你是如何优化数据库的.

  • 打赏
  • 举报
回复
因人而异,看什么岗位,出什么题!
fcuandy 2006-07-03
  • 打赏
  • 举报
回复
请问楼主的,这些题答对了能拿多少钱?我比较俗
铁歌 2006-07-03
  • 打赏
  • 举报
回复
二 SQLSERVER的实现部分

1 有订单表,需要实现它的编号,格式如下:200211030001……200222039999等

2 有表T1,T2,现有一事务,在向表T1添加数据时,同时也必须向T2也添加数据,如何实现该事务

3 如何向T1中的编号字段(code varchar(20))添加一万条记录,不充许重复,规则如下:编号的数据必须从小写的a-z之间取值

4 如何删除表中的重复数据,请使用游标与分组的办法

5 如何求表中相邻的两条记录的某字段的值之差

6 如何统计数据库中所有用户表的数据,显示格式如下:
表名 记录数
sales 23

7 如何删除数据库中的所有用户表(表与表之间有外键关系)

8 表A editor_id       lb2_id
   123           000
   123           003
   123           003
   456           007
   456           006
表B  lb2_id         lb2_name
    000           a
    003           b
    006           c
    007           d
显示 a   共1条 (表A内lb2_id为000的条数)
   b   共2条(表A内lb2_id为003的条数)

9 人员情况表(employee):里面有一字段文化程度(wh):包括四种情况(本科以上,大专,高中,初中以下),现在我要根据年龄字段查询统计出:表中文化程度为本科以上,大专,高中,初中以下,各有多少人,占总人数多少。
SELECT wh AS 学历,age as 年龄, Count(*) AS 人数,
Count(*) * 100 /(SELECT Count(*) FROM employee) AS 百分比
FROM employee GROUP BY wh,age
学历 年龄 人数 百分比
本科以上 20 34 14
大专 20 33 13
高中 20 33 13
初中以下 20 100 40
本科以上 21 50 20

10 现在有三个表student:(FID 学生号,FName 姓名),
subject:(FSubID 课程号,FSubName 课程名),
Score(FScoreId 成绩记录号,FSubID 课程号,FStdID 学生号,FScore 成绩)
怎么能实现这个表:
姓名 英语 数学 语文 历史
张萨 78 67 89 76
王强 89 67 84 96

SELECT a.FName AS 姓名,
英语 = SUM(CASE b.FSubName WHEN '英语' THEN c.FScore END),
数学 = SUM(CASE b.FSubName WHEN '数学' THEN c.FScore END),
语文 = SUM(CASE b.FSubName WHEN '语文' THEN c.FScore END),
历史 = SUM(CASE b.FSubName WHEN '历史' THEN c.FScore END)
FROM Student a, Subject b, Score c
WHERE a.FID = c.FStdId AND b.FSubID = c.FsubID GROUP BY a.FName

11 原始表的数据如下:
PID PTime PNo
111111 2003-01-28 04:30:09
111111 2003-01-28 18:30:00
222222 2003-01-28 04:31:09
333333 2003-01-28 04:32:09
111111 2003-02-09 03:35:25
222222 2003-02-09 03:36:25
333333 2003-02-09 03:37:25

查询生成表
PDate 111111 222222 333333 ......
2003-01-28 04:30:09 04:31:09 04:32:09 ......
2003-01-28 18:30:00
2003-02-09 03:35:25 03:36:25 03:37:25 ......

12 表一(AAA)
商品名称mc 商品总量sl
A 100
B 120
表二(BBB)
商品名称mc 出库数量sl
A 10
A 20
B 10
B 20
B 30

用一条SQL语句算出商品A,B目前还剩多少?

declare @AAA table (商品名称 varchar(10), 商品总量 int)
insert into @AAA values('A',100)
insert into @AAA values('B',120)

declare @BBB table (商品名称 varchar(10), 出库数量 int)
insert into @BBB values('A', 10)
insert into @BBB values('A', 20)
insert into @BBB values('B', 10)
insert into @BBB values('B', 20)
insert into @BBB values('B', 30)

select TA.商品名称,A-B AS 剩余数量 FROM
(select 商品名称,sum(商品总量) AS A
from @AAA
group by 商品名称)TA,
(select 商品名称,sum(出库数量) AS B
from @BBB
group by 商品名称)TB
where TA.商品名称=TB.商品名称

select 商品名称,sum(商品总量) 剩余数量 from (select * from @aaa union all select 商品名称,-出库数量 from @bbb) a group by 商品名称

13 优化这句SQL语句
UPDATE tblExlTempYear
SET tblExlTempYear.GDQC = tblExlTempMonth.GDQC
FROM tblExlTempYear,tblExlTempMonth
where tblExlTempMonth.GDXM=tblExlTempYear.GDXM and tblExlTempMonth.TXDZ=tblExlTempYear.TXDZ

(1)、加索引:
tblExlTempYear(GDXM,TXDZ)
tblExlTempMonth (GDXM,TXDZ)
(2)、删除无用数据
(3)、转移过时数据
(4)、加服务器内存,升级服务器
(5)、升级网络系统

UPDATE tblExlTempYear
SET tblExlTempYear.GDQC = tblExlTempMonth.GDQC
FROM tblExlTempYear (index indexY),tblExlTempMonth (index indexM)
where tblExlTempMonth.GDXM=tblExlTempYear.GDXM and tblExlTempMonth.TXDZ=tblExlTempYear.TXDZ

14 品种 日期 数量
P0001 2002-1-10 10
P0001 2002-1-10 11
P0001 2002-1-10 50
P0001 2002-1-12 9
P0001 2002-1-12 8
P0001 2002-1-12 7
P0002 2002-10-10 5
P0002 2002-10-10 7
P0002 2002-10-12 0.5
P0003 2002-10-10 5
P0003 2002-10-12 7
P0003 2002-10-12 9

结果要先按照品种汇总,再按照日期汇总,结果如下:
P0001 2002-1-10 71
P0001 2002-1-12 24
P0002 2002-10-10 12
P0002 2002-10-12 0.5
P0003 2002-10-10 5
P0003 2002-10-12 16

SQL SERVER能做出这样的汇总吗…

15 在分組查循中with{cube|rollup}的區別是什么?
如:
use pangu
select firm_id,p_id,sum(o_price_quantity)as sum_values
from orders
group by firm_id,p_id
with cube

use pangu
select firm_id,p_id,sum(o_price_quantity)as sum_values
from orders
group by firm_id,p_id
with rollup
的區別是什么?

CUBE 和 ROLLUP 之间的区别在于:
CUBE 生成的结果集显示了所选列中值的所有组合的聚合。
ROLLUP 生成的结果集显示了所选列中值的某一层次结构的聚合。
例如,简单表 Inventory 中包含:
Item Color Quantity
-------------------- -------------------- --------------------------
Table Blue 124
Table Red 223
Chair Blue 101
Chair Red 210




下列查询将生成小计报表:
SELECT CASE WHEN (GROUPING(Item) = 1) THEN 'ALL'
ELSE ISNULL(Item, 'UNKNOWN')
END AS Item,
CASE WHEN (GROUPING(Color) = 1) THEN 'ALL'
ELSE ISNULL(Color, 'UNKNOWN')
END AS Color,
SUM(Quantity) AS QtySum
FROM Inventory
GROUP BY Item, Color WITH ROLLUP

Item Color QtySum
-------------------- -------------------- --------------------------
Chair Blue 101.00
Chair Red 210.00
Chair ALL 311.00
Table Blue 124.00
Table Red 223.00
Table ALL 347.00
ALL ALL 658.00
(7 row(s) affected)
如果查询中的 ROLLUP 关键字更改为 CUBE,那么 CUBE 结果集与上述结果相同,只是在结果集的末尾还会返回下列两行:
ALL Blue 225.00
ALL Red 433.00
CUBE 操作为 Item 和 Color 中值的可能组合生成行。例如,CUBE 不仅报告与 Item 值 Chair 相组合的 Color 值的所有可能组合(Red、Blue 和 Red + Blue),而且报告与 Color 值 Red 相组合的 Item 值的所有可能组合(Chair、Table 和 Chair + Table)。对于 GROUP BY 子句中右边的列中的每个值,ROLLUP 操作并不报告左边一列(或左边各列)中值的所有可能组合。例如,ROLLUP 并不对每个 Color 值报告 Item 值的所有可能组合。ROLLUP 操作的结果集具有类似于 COMPUTE BY 所返回结果集的功能;然而,
ROLLUP 具有下列优点: ROLLUP 返回单个结果集;COMPUTE BY 返回多个结果集,而多个结果集会增加应用程序代码的复杂性。ROLLUP 可以在服务器游标中使用;COMPUTE BY 不可以。有时,查询优化器为 ROLLUP 生成的执行计划比为 COMPUTE BY 生成的更为高效。


billpu 2006-07-03
  • 打赏
  • 举报
回复
今早有两
http://community.csdn.net/Expert/topic/4235/4235824.xml?temp=.2695734
http://community.csdn.net/Expert/topic/4744/4744212.xml?temp=.4091913
Dong 2006-07-03
  • 打赏
  • 举报
回复
自己google啊,

Question 1:Can you use a batch SQL or store procedure to calculating the Number of Days in a Month
Answer 1:找出当月的天数
select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01' as datetime))))

Question2:Can you use a SQL statement to calculating it!
How can I print "10 to 20" for books that sell for between $10 and $20,"unknown" for books whose price is null, and "other" for all other prices?
Answer 2:
select bookid,bookname,price=case when price is null then 'unknown'
when price between 10 and 20 then '10 to 20' else price end
from books

Question3:Can you use a SQL statement to finding duplicate values!
How can I find authors with the same last name?
You can use the table authors in datatabase pubs. I want to get the result as below:
Output:
au_lname number_dups
---------------------------------------- -----------
Ringer 2
(1 row(s) affected)
Answer 3
select au_lname,number_dups=count(1) from authors group by au_lname

Question4:Can you create a cross-tab report in my SQL Server!
How can I get the report about sale quality for each store and each quarter and the total sale quality for each quarter at year 1993?
You can use the table sales and stores in datatabase pubs.
Table Sales record all sale detail item for each store. Column store_id is the id of each store, ord_date is the order date of each sale item, and column qty is the sale qulity. Table stores record all store information.
I want to get the result look like as below:
Output:
stor_name Total Qtr1 Qtr2 Qtr3 Qtr4
---------------------------------------- ----------- ----------- ----------- ----------- -----------
Barnum's 50 0 50 0 0
Bookbeat 55 25 30 0 0
Doc-U-Mat: Quality Laundry and Books 85 0 85 0 0
Fricative Bookshop 60 35 0 0 25
Total 250 60 165 0 25

Answer 4:用动态SQL实现

Question5: The Fastest Way to Recompile All Stored Procedures
I have a problem with a database running in SQL Server 6.5 (Service Pack 4). We moved the database (object transfer) from one machine to another last night, and an error (specific to a stored procedure) is cropping up. However, I can't tell which procedure is causing it. Permissions are granted in all of our stored procedures; is there a way from the isql utility to force all stored procedures to recompile?

Tips: sp_recompile can recomplie a store procedure each time
Answer 5:在执行存储过程时,使用 with recompile 选项强制编译新的计划;使用sp_recompile系统存储过程强制在下次运行时进行重新编译

Question6: How can I add row numbers to my result set?
In database pubs, have a table titles , now I want the result shown as below,each row have a row number, how can you do that?
Result:
line-no title_id
----------- --------
1 BU1032
2 BU1111
3 BU2075
4 BU7832
5 MC2222
6 MC3021
7 MC3026
8 PC1035
9 PC8888
10 PC9999
11 PS1372
12 PS2091
13 PS2106
14 PS3333
15 PS7777
16 TC3218
17 TC4203
18 TC7777

Answer 6:
--SQL 2005的写法
select row_number() as line_no ,title_id from titles
--SQL 2000的写法
select line_no identity(int,1,1),title_id into #t from titles
select * from #t
drop table #t

Question 7: Can you tell me what the difference of two SQL statements at performance of execution?
Statement 1:
if NOT EXISTS ( select * from publishers where state = 'NY')
begin
SELECT 'Sales force needs to penetrate New York market'
end
else
begin
SELECT 'We have publishers in New York'
end
Statement 2:
if EXISTS ( select * from publishers where state = 'NY')
begin
SELECT 'We have publishers in New York'
end
else
begin
SELECT 'Sales force needs to penetrate New York market'
end
Answer 7:不同点:执行时的事务数,处理时间,从客户端到服务器端传送的数据量大小

Question8: How can I list all California authors regardless of whether they have written a book?
In database pubs, have a table authors and titleauthor , table authors has a column state, and titleauhtor have books each author written.
CA behalf of california in table authors.
Answer 8:
select * from authors where state='CA'

Question9: How can I get a list of the stores that have bought both 'bussiness' and 'mod_cook' type books?
In database pubs, use three table stores,sales and titles to implement this requestment.
Now I want to get the result as below:
stor_id stor_name
------- ----------------------------------------
...
7896 Fricative Bookshop
...
...
...
Answer 9:
select distinct a.stor_id, a.stor_name from stores a,sales b,titles c
where a.stor_id=b.stor_id and b.title_id=c.title_id and c.type='business' and
exists(select 1 from sales k,titles g where stor_id=b.stor_id
and k.title_id=g.title_id and g.type='mod_cook')



Question10: How can I list non-contignous data?
In database pubs, I create a table test using statement as below, and I insert several row as below
create table test
( id int primary key )
go

insert into test values (1 )
insert into test values (2 )
insert into test values (3 )
insert into test values (4 )
insert into test values (5 )
insert into test values (6 )
insert into test values (8 )
insert into test values (9 )
insert into test values (11)
insert into test values (12)
insert into test values (13)
insert into test values (14)
insert into test values (18)
insert into test values (19)
go

Now I want to list the result of the non-contignous row as below,how can I do it?
Missing after Missing before
------------- --------------
6 8
9 11
...




Answer 10:
select id from test t where not exists(select 1 from test where id=t.id+1)
or not exists(select 1 from test where id=t.id-1)

Question11: How can I list all book with prices greather than the average price of books of the same type?
In database pubs, have a table named titles , its column named price mean the price of the book, and another named type mean the type of books.
Now I want to get the result as below:
type title price
------------ -------------------------------------------------------------------------------- ---------------------
business The Busy Executive's Database Guide 19.9900
...
...
...
...

Answer 11:
select a.type,a.title,a.price from titles a,
(select type,price=avg(price) from titles group by type)b
where a.type=b.type and a.price>b.price

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧