某外企SQL Server面试题

原文给出了部分答案,本人在没有任何外界压力的情况下,用 sqlserver 和 oracle 各做了一遍,感觉有难度。要是真的去考试,估计没希望了。还要继续努力啊!

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))));

(Oracle)
select to_number(to_char(last_day(sysdate),'DD')) from dual;

 

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 'other' end
from books;

(Oracle)
select bookid,bookname,case when to_char(price) is null then
                'unknow' 
            when to_char(price) between '10' and '20' then
                '10 to 20'
            else
                'other'    
        end "price"
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;

(Oracle)
select au_lname,count(1) as "number_dups" 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:
declare @s varchar(8000)
set @s='select isnull(stores.stor_name,''Total'') '
select @s=@s + ',[Qtr' + cast(datepart(q,ord_date) as char(1)) + ']=sum(case when datepart(q,ord_date)=''' + cast(datepart(q,ord_date) as char(1)) + ''' then qty else 0 end)'
from Sales
group by datepart(q,ord_date)
set @s=@s + ' ,sum(qty) "Total" from Sales inner join stores on Sales.store_id = stores.store_id '
set @s=@s + ' where datepart(yyyy,ord_date) = ''1993'' '
set @s=@s + ' group by stores.stor_name WITH ROLLUP '
set @s=@s + ' order by stores.stor_name '
exec(@s)

(Oracle)
CREATE OR REPLACE PACKAGE pkg_getrecord
IS
    TYPE myrctype IS REF CURSOR;
END pkg_getrecord;
/
CREATE OR REPLACE FUNCTION fn_rs
    RETURN pkg_getrecord.myrctype
IS
    s VARCHAR2 (4000);

    CURSOR c1
    IS
        SELECT ',sum(case when to_char(ord_date,''q'')='
            || TO_CHAR (ord_date, 'q')
            || ' then qty else 0 end)'
            || ' "Qtr'
            || TO_CHAR (ord_date, 'q')
            || '"' c2
        FROM sales
        GROUP BY TO_CHAR (ord_date, 'q');

    r1 c1%ROWTYPE;
    list_cursor pkg_getrecord.myrctype;
BEGIN
    s := 'select nvl(stores.stor_name,''Total'')';

    OPEN c1;

    LOOP
        FETCH c1
        INTO r1;

        EXIT WHEN c1%NOTFOUND;
            s := s || r1.c2;
    END LOOP;

    CLOSE c1;

    s :=
    s
        || ',sum(qty) "Total" from Sales join stores on Sales.store_id = stores.store_id where to_char(ord_date,''yyyy'') =     ''1993'''
        || ' group by rollup(stores.stor_name) order by stores.stor_name';

    OPEN list_cursor FOR s;

    RETURN list_cursor;
END fn_rs;
/

var results refcursor;
exec :results := fn_rs;
print results;


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系统存储过程强制在下次运行时进行重新编译

Oracle
不存在上面的情况,Oracle自动检查 invalid 存储过程


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() over(order by title_id) 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

(Oracle)
select rownum as line_no, title_id from titles;



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:
不同点:执行时的事务数,处理时间,从客户端到服务器端传送的数据量大小

(Oracle)同上



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';

(Oracle)同上


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');

(Oracle)同上




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 t1."missing after", min(t2."missing before") "missing before" from 
(select a "missing after" from test t1 where not exists (select 1 from test t2 where t1.a = t2.a - 1) 
and a != (select max(a) from test)) t1,
(select a "missing before" from test t1 where not exists (select 1 from test t2 where t1.a = t2.a + 1) 
and a != (select min(a) from test)) t2
where t1."missing after" < t2."missing before"
group by t1."missing after"
order by 1,2;

(Oracle)同上



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;

(Oracle)
select a.type,a.title,a.price from titles a,
(select type,avg(price) price from titles group by type) b
where a.type=b.type and a.price>b.price;


试题点评:通览整个试题,我们不难发现,这份试题是针对SQL Server数据库人员的。而从难度分析上来看,这份试题也属于同类试题中比较难的了。之所以说它难,首先是限定时间的全英文试题;其次,尽管这份试题主要是考核开发能力,但却涉及到了算法的选择和性能的调优;最后,这份试题还夹进了SQL Server数据库的升级问题。因此,综上所述,我们估计这是一家从事程序外包工作的外企招聘后台开发或与后台开发相关的SQL Server高级程序员的试题。
 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值