SQL Server interview questions for a foreign company

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:
Select datepart (dd, dateadd (dd,-1, dateadd (mm, 1, 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 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' 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) = '000000 '''
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') = ''1100 '''
| '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 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 forcibly compile the new plan; Use sp_recompile system stored procedure to force re-Compile at the next run.

Oracle
The preceding situation does not exist. Oracle automatically checks the invalid stored procedure.


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

(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:
Difference: the number of transactions during execution, the processing time, and the amount of data transferred from the client to the server

(Oracle) Same as above



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) Same as above


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) Same as above

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! = (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! = (Select min (a) from test) t2
Where T1. "missing after" <t2. "missing before"
Group by T1. "missing after"
Order by 1, 2;

(Oracle) Same as above

Question11:How can I list all books 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,
(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,
(Select type, AVG (price) price from titles group by type) B
Where a. type = B. Type and A. Price> B. price;


Comments: it is not difficult to find that this question is intended for SQL Server database personnel. From the difficulty analysis, this question is also more difficult than similar questions. It is difficult to say that the first is a time-limited full-English question. Secondly, although this question is mainly used to assess the development ability, it involves algorithm selection and performance tuning; finally, this question is also included in the SQL Server Database Upgrade question. Therefore, to sum up, we estimate that this is a foreign company engaged in program outsourcing job recruiting Background Development or SQL Server senior programmers related to background development questions.

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.