1. 程式人生 > >使用SQL語句完成下列題目

使用SQL語句完成下列題目

1、查詢圖書館中所有圖書的詳細資訊。(BookInfo表)

1

select from bookinfo;

  

2、查詢所有圖書的圖書編號、圖書名稱和圖書價格。

1

select b_id 編號,b_name 名稱,b_price 價格 from bookinfo

  

3、 查詢所有圖書的圖書編號、圖書名稱和圖書總額。

1

select

 b_id 編號,b_name 名稱,b_price*b_quantity 價格 from bookinfo

  

4、查詢所有圖書的圖書編號、圖書名稱和總價值,但希望以漢字標題圖書編號、圖書名稱和總價值表示b_IDb_Nameb_Price*b_Quantity

1

select b_id 編號,b_name 名稱,b_price*b_quantity 價格 from bookinfo

  

5、查詢所有圖書中的“劉志成”編寫的圖書的所有資訊。

1

select from bookinfo where b_author like '%劉志成%'

6、 查詢圖書類別編號為“17”,圖書價格在25~30元之間的圖書資訊,要求以漢字標題顯示圖書編號、圖書名稱、圖書類別編號和圖書價格。

1

select b_id 圖書編號,b_name 圖書名稱,bt_id 圖書類別編號,b_price 圖書價格 <br>from bookinfo 

where bt_id = 17 and b_price between 25 and 30;

7、 查詢所有入庫在5年以上並且圖書價格在10~20之間的圖書的名稱、圖書作者、圖書價格和入庫時間(用“入庫時長”表示,該列不是基本表中的欄位,是計算出來的列)。

1

2

3

4

5

6

7

select b_name 圖書名稱,b_author 圖書作者,b_price 圖書價格, b_date 入庫時間

 

from bookinfo

 

where ROUND(TO_NUMBER(SYSDATE - b_date))>=5

 

and b_price between 10 and 20;

  

8、 查詢所有入庫在5年以上並且圖書價格不在10~20之間的圖書的名稱、圖書作者、圖書價格和入庫時間

1

2

3

4

5

6

7

select b_name 圖書名稱,b_author 圖書作者,b_price 圖書價格, b_date 入庫時間

 

from bookinfo

 

where ROUND(TO_NUMBER(SYSDATE - b_date))>=5

 

and b_price not between 10 and 20;

  

9、查詢出版社編號為“001”和“003”的圖書詳細資訊

1

select from bookinfo where p_id in (001,003)

  

10、查詢圖書名稱中包含“資料庫”字樣的圖書的詳細資訊。

1

select from bookinfo where b_name like '%資料庫%'

  

11、查詢姓“王”且名字中有3個漢字的讀者的編號、讀者的姓名及可借書數量。

1

2

3

select r_id,r_name,r_quantity from readerinfo

 

where r_name like '王__';

  

12、查詢暫時沒有圖書封面圖片的圖書資訊。

1

select from bookinfo where b_pricture is null;

  

13、       通過圖書管理系統查詢借閱圖書的讀者編號,如果一個讀者借閱了多本圖書,只需要顯示一次讀者編號。

1

select distinct r_id from borrowreturn;

  

14、查詢前5行圖書的詳情資訊

1

select from bookinfo where rownum <6;

  

15、查詢前20%行圖書的詳情資訊。

1

2

3

4

5

select *

 

from bookinfo

 

where rownum <to_number(0.2*(select count(*)from bookinfo)+1);

  

 

16、查詢圖書出版社編號為“001”的圖書的圖書編號、圖書名稱、圖書價格和出版社編號,並要求根據圖書價格進行降序排列。

1

2

3

4

5

select b_id 圖書編號,b_name 圖書名稱,b_price 圖書價格,p_id 圖書出版社編號

 

from (select from bookinfo order by b_price desc)

 

where p_id = 001;

  

17、查詢價格在20元以上的圖書的圖書編號、圖書名稱、圖書價格和出版社編號的資訊,並要求按出版社編號升序排列;如果是同一出版社的圖書,則按價格降序排列。

1

2

3

select b_id 圖書編號,b_name 圖書名稱,b_price 圖書價格,p_id 圖書出版社編號

 

from bookinfo order by p_id asc,b_price desc;

  

18、查詢圖書館中每一個出版社的圖書種類數。

1

2

3

4

5

6

7

select p_id 出版社編號,count(p_id) 圖書種類

 

from bookinfo

 

group by p_id

 

order by p_id;

  

19、查詢圖書館中每一個出版社的圖書種類數及圖書總數。

1

2

3

4

5

6

7

select p_id 出版社編號,count(p_id) 圖書種類,sum(b_quantity) 圖書總數

 

from bookinfo

 

group by p_id

 

order by p_id;

  

20、分組後的資料按圖書總數進行降序排列。

1

2

3

4

5

6

7

select p_id 出版社編號,count(p_id) 圖書種類,sum(b_quantity) 圖書總數

 

from bookinfo

 

group by p_id

 

order by sum(b_quantity) desc;

  

21、對分組後的資料進行篩選,要求顯示圖書總量大於15的出版社編號、圖書種類和圖書總數,篩選後的結果按“圖書總數”升序排列。

1

2

3

4

5

6

7

8

9

select p_id 出版社編號,count(p_id) 圖書種類,sum(b_quantity) 圖書總數

 

from bookinfo

 

group by p_id

 

having sum(b_quantity)>15

 

order by sum(b_quantity) asc;

  

22、查詢每種圖書的圖書編號、圖書名稱和圖書型別名稱(不是圖書類別編號)。

1

2

3

4

5

select b_id 圖書編號, b_name 圖書名稱,bt_name 圖書型別名稱

 

from bookinfo,booktype

 

where bookinfo.bt_id = booktype.bt_id

  

 

1

2

3

4

5

select b_id 圖書編號, b_name 圖書名稱,bt_name 圖書型別名稱

 

from bookinfo join booktype

 

on bookinfo.bt_id = booktype.bt_id

  

23、查詢借還表中所有“未還”圖書的借閱ID、借閱人(讀者)、圖書名稱、詳細存放位置和借出日期。

1

2

3

4

5

6

7

8

9

10

11

select borrowreturn.r_id 借閱ID, r_name 借閱人,b_name 圖書名稱,s_position

 

from borrowreturn

 

join readerinfo on borrowreturn.r_id=readerinfo.r_id

 

join bookstore on borrowreturn.s_id=bookstore.s_id

 

join bookinfo on bookstore.b_id=bookinfo.b_id

 

where br_status = '未還';

  

24、查詢不低於《JSP程式設計安全教程》價格的圖書編號、圖書名稱和圖書價格,查詢後的結果要求按圖書價格升序排列。

1

2

3

4

5

6

7

select b_id 圖書編號,b_name 圖書名稱,b_price 圖書價格

 

from bookinfo

 

where b_price>=(select b_price from bookinfo where b_name = 'JSP程式設計案例教程')

 

order by b_price asc;

  

 

 

1

2

3

4

5

6

7

select G2.b_id 圖書編號,G2.b_name 圖書名稱,G2.b_price 圖書價格

 

from bookinfo G1 join bookinfo G2 on G1.b_name = 'JSP程式設計案例教程'

 

and G1.b_Price<=G2.b_price

 

order by G2.b_price;

  

25、查詢所有圖書類別及其對應圖書資訊,如果該圖書類別沒有對應圖書也需要顯示其類別資訊。將BookType表和BookInfo表進行左外連線,BookType為左表,BookInfo表為右表。

1

2

3

4

5

6

7

select G2.b_id 圖書編號,G2.b_name 圖書名稱,G2.b_price 圖書價格

 

from bookinfo G1 join bookinfo G2 on G1.b_name = 'JSP程式設計案例教程'

 

and G1.b_Price<=G2.b_price

 

order by G2.b_price;

  

26、查詢所有圖書的資訊(即使是不存在對應的圖書類別資訊,實際上這種情況是不存在的)。

1

2

3

4

5

select booktype.bt_id,bt_name,b_id,b_name,b_price,b_quantity

 

from booktype left join bookinfo

 

on booktype.bt_id = bookinfo.bt_id

  

27、查詢和《UML使用者指南》為同一出版社的圖書的圖書編號、圖書名稱和出版社編號。

1

2

3

4

5

select b_id 圖書編號,b_name 圖書名稱,p_id 出版社編號

 

from bookinfo

 

where p_id =(select p_id from bookinfo where b_name = 'UML使用者指南')

  

28、查詢借閱了《SQL Server 2005例項教程》的借閱號、借閱日期和操作員。

1

2

3

4

5

select s_id 借閱號,br_outdate 借閱日期,br_operator 操作員

 

from borrowreturn

 

where s_id in(select s_id from bookstore where b_id in(select b_id from bookinfo where b_name = 'SQL Server 2005例項教程'));

  

 

1

2

3

4

5

6

7

8

9

select borrowreturn.s_id 借閱號,br_outdate 借閱日期,br_operator 操作員

 

from borrowreturn

 

join bookstore on borrowreturn.s_id = bookstore.s_id

 

join bookinfo on bookstore.b_id = bookinfo.b_id

 

where b_name = 'SQL Server 2005例項教程';

  

29、查詢比出版社編號為“007”任一圖書入庫日期晚的圖書資訊,查詢結果按降序排列。

1

2

3

4

5

6

7

8

9

select b_id,b_name,b_date,b_price

 

from bookinfo

 

where b_date>all

 

(select b_date from bookinfo where p_id='007')

 

order by b_date desc;

  

 

1

2

3

4

5

6

7

select b_id,b_name,b_date,b_price

 

from bookinfo

 

where b_date>(select max(b_date) from bookinfo where p_id='007')

 

order by b_date desc;

  

30、針對ReadInfo表中的每一位讀者,在BorrowReturn表中查詢借閱過圖書並且圖書狀態為“已還”的所有借閱資訊。

1

2

3

4

5

6

7

8

9

select br_id,s_id,br_outdate,br_indate

 

from borrowreturn

 

where br_status = '已還'

 

and exists

 

(select from readerinfo where readerinfo.r_id=borrowreturn.r_id);

  

31、求每一類別圖書的平均價格,並將結果儲存到資料庫中。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

create table avgbookprice(

 

bt_id char(10),

 

p_avg number(7,2)

 

);

 

insert into avgbookprice(bt_id,p_avg)

 

select bt_id,avg(b_price)

 

from bookinfo

 

group by bt_id;

  

32、將圖書管理系統中的圖書出版社名稱為“機械工業出版社”的圖書的數量加1.

1

2

3

4

5

update bookinfo set b_quantity=b_quantity + 1

 

where p_id = (select p_id from publisher

 

where p_name = '機械工業出版社');

  

33、刪除出版社名稱為“機械工業出版社”的所有圖書資訊。

1

2

3

4

5

delete from bookinfo

 

where p_id =

 

(select p_id from publisher where p_name = '機械工業出版社')