最近练习的一份SQL题目,很经典也有点点水平,给大家瞧瞧

1       Question1

Can you use a batch SQL or store procedure to calculating the Number of Days in a Month?

I am trying to find a way to calculate the total number of days in each month.

I am trying to avoid using a case statement, as it fails for February on leap years.

Is there a way to do it using the date functions in SQL Server?

2       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?

You can use the table titles in database pubs.

3       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 database pubs. I want to get the result as below:

 

Output:

au_lname                   number_dups

---------------------------------------- -----------

Ringer                      2

 

(1 row(s) affected)

4       Question4

Can you create a cross-tab report in my SQL Server?

How can I get the report about sale quantity for each store and each quarter

and the total sale quantity for each quarter at year 1993?

 

You can use the table sales and stores in database 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 quantity.

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

 

5       Question5

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

 

6       Question6

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.

7       Question7

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

...

...

...

8       Question8

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

...

9       Question9

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

...

...

...

...

 

 

posted @ 2010-07-06 20:04  以风之名  阅读(778)  评论(2)    收藏  举报