某外企SQL Server面試題(英文,答案)

來源:互聯網
上載者:User

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

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.