使用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_ID、b_Name和b_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 = '機械工業出版社')
