使用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 = '机械工业出版社')

猜你喜欢

转载自blog.csdn.net/do_you_ac_today/article/details/83829282