设为首页 收藏本站
查看: 1563|回复: 0

[经验分享] sql语句查询作业

[复制链接]

尚未签到

发表于 2017-7-14 15:36:12 | 显示全部楼层 |阅读模式
  使用SQL语句完成下列题目
  1、            查询图书馆中所有图书的详细信息。(BookInfo表)



select * from bookinfo;

  2、            查询所有图书的图书编号、图书名称和图书价格。



select b_id 编号,b_name 名称,b_price 价格 from bookinfo

  3、            查询所有图书的图书编号、图书名称和图书总额。



select b_id 编号,b_name 名称,b_price*b_quantity 价格 from bookinfo

  4、            查询所有图书的图书编号、图书名称和总价值,但希望以汉字标题图书编号、图书名称和总价值表示b_IDb_Nameb_Price*b_Quantity



select b_id 编号,b_name 名称,b_price*b_quantity 价格 from bookinfo

  5、            查询所有图书中的“刘志成”编写的图书的所有信息。



select * from bookinfo where b_author like '%刘志成%'

  6、            查询图书类别编号为“17”,图书价格在25~30元之间的图书信息,要求以汉字标题显示图书编号、图书名称、图书类别编号和图书价格。



select b_id 图书编号,b_name 图书名称,bt_id 图书类别编号,b_price 图书价格
from bookinfo where bt_id = 17 and b_price between 25 and 30;

  7、            查询所有入库在5年以上并且图书价格在10~20之间的图书的名称、图书作者、图书价格和入库时间(用“入库时长”表示,该列不是基本表中的字段,是计算出来的列)。



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之间的图书的名称、图书作者、图书价格和入库时间



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”的图书详细信息



select * from bookinfo where p_id in (001,003)

  10、       查询图书名称中包含“数据库”字样的图书的详细信息。



select * from bookinfo where b_name like '%数据库%'

  11、       查询姓“王”且名字中有3个汉字的读者的编号、读者的姓名及可借书数量。



select r_id,r_name,r_quantity from readerinfo
where r_name like '王__';

  12、       查询暂时没有图书封面图片的图书信息。



select * from bookinfo where b_pricture is null;

  13、       通过图书管理系统查询借阅图书的读者编号,如果一个读者借阅了多本图书,只需要显示一次读者编号。



select distinct r_id from borrowreturn;

  14、       查询前5行图书的详情信息



select * from bookinfo where rownum <6;

  15、       查询前20%行图书的详情信息。



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

  16、       查询图书出版社编号为“001”的图书的图书编号、图书名称、图书价格和出版社编号,并要求根据图书价格进行降序排列。



select b_id 图书编号,b_name 图书名称,b_price 图书价格,p_id 图书出版社编号
from (select * from bookinfo order by b_price desc)
where p_id = 001;

  17、       查询价格在20元以上的图书的图书编号、图书名称、图书价格和出版社编号的信息,并要求按出版社编号升序排列;如果是同一出版社的图书,则按价格降序排列。



select b_id 图书编号,b_name 图书名称,b_price 图书价格,p_id 图书出版社编号
from bookinfo order by p_id asc,b_price desc;

  18、       查询图书馆中每一个出版社的图书种类数。



select p_id 出版社编号,count(p_id) 图书种类
from bookinfo
group by p_id
order by p_id;

  19、       查询图书馆中每一个出版社的图书种类数及图书总数。



select p_id 出版社编号,count(p_id) 图书种类,sum(b_quantity) 图书总数
from bookinfo
group by p_id
order by p_id;

  20、       分组后的数据按图书总数进行降序排列。



select p_id 出版社编号,count(p_id) 图书种类,sum(b_quantity) 图书总数
from bookinfo
group by p_id
order by sum(b_quantity) desc;

  21、       对分组后的数据进行筛选,要求显示图书总量大于15的出版社编号、图书种类和图书总数,筛选后的结果按“图书总数”升序排列。



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、       查询每种图书的图书编号、图书名称和图书类型名称(不是图书类别编号)。



select b_id 图书编号, b_name 图书名称,bt_name 图书类型名称
from bookinfo,booktype
where bookinfo.bt_id = booktype.bt_id




select b_id 图书编号, b_name 图书名称,bt_name 图书类型名称
from bookinfo join booktype
on bookinfo.bt_id = booktype.bt_id

  23、       查询借还表中所有“未还”图书的借阅ID、借阅人(读者)、图书名称、详细存放位置和借出日期。



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程序设计安全教程》价格的图书编号、图书名称和图书价格,查询后的结果要求按图书价格升序排列。



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;




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表为右表。



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、       查询所有图书的信息(即使是不存在对应的图书类别信息,实际上这种情况是不存在的)。



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用户指南》为同一出版社的图书的图书编号、图书名称和出版社编号。



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实例教程》的借阅号、借阅日期和操作员。



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实例教程'));





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”任一图书入库日期晚的图书信息,查询结果按降序排列。



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;




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表中查找借阅过图书并且图书状态为“已还”的所有借阅信息。



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、       求每一类别图书的平均价格,并将结果保存到数据库中。



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.



update bookinfo set b_quantity=b_quantity + 1
where p_id = (select p_id from publisher
where p_name = '机械工业出版社');

  33、       删除出版社名称为“机械工业出版社”的所有图书信息。



delete from bookinfo
where p_id =
(select p_id from publisher where p_name = '机械工业出版社')

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-393838-1-1.html 上篇帖子: SQL语句(建库、建表、修改语句) 下篇帖子: SQL Server 在task manager里面显示CPU 使用率过高
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表