SQL Server interview questions for a foreign company (English, answer)

Source: Internet
Author: User

Question 1: Can you use a batch SQL or store procedure to calculating the number of days in a month

Answer 1: Find the number of days in the current month
Select datepart (DD, dateadd (DD,-1, dateadd (mm, 1, 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 require 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: implemented using dynamic 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 R Ecompile?

TIPS: sp_recompile can recomplie a store procedure each time

Answer 5: When executing a stored procedure, use the with recompile option to force the compilation of a new plan; Use sp_recompile to force the re-compilation at the next run.

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 statement
Select row_number () as line_no, title_id from titles
-- SQL 2000 statement
Select line_no identity (INT, 1, 1), title_id into # T from titles
Select * from # T
Drop table # T

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.