先创建下面三个表:
                                                          (book表)
Oracle sql语句练习_休闲
 
                                (borrow表)
Oracle sql语句练习_休闲_02
 
                                                         (reader表)
Oracle sql语句练习_数据库_03
 
1) 找出姓李的读者姓名(NAME)和所在单位(COMPANY)。
SELECT name,addr FROM reader WHERE name LIKE '李%';
 
2) 列出图书库中所有藏书的书名(BOOK_NAME)及出版单位(OUTPUT)。
SELECT DISTINCT(book_name),output FROM book; 
 
3) 查找“高等教育出版社”的所有图书名称(BOOK_NAME)及单价(PRICE),结果按单价降序排序。
SELECT DISTINCT(book_name),price FROM book
    WHERE output='高等教育出版社'
    ORDER BY 2 DESC;
 
4) 查找价格介于10元和20元之间的图书种类(SORT),结果按出版单位(OUTPUT)和单价(PRICE)升序排序。
SELECT sort,price FROM book 
     WHERE price BETWEEN 10 AND 20
     ORDER BY output,price;
 
5) 查找书名以”计算机”开头的所有图书和作者(WRITER)。
SELECT DISTINCT(book_name),writer FROM book 
     WHERE book_name LIKE '计算机%';
 
6) 检索同时借阅了总编号(BOOK_ID)为112266和449901两本书的借书证号(READER_ID)。
SELECT borrow.reader_id FROM borrow ,
     (SELECT reader_id FROM borrow WHERE book_id = 112266) t WHERE borrow.reader_id = t.reader_id and book_id = 449901;
 
7)* 查找所有借了书的读者的姓名(NAME)及所在单位(COMPANY)。
SELECT name,company FROM reader
WHERE reader_id in
     (SELECT reader_id FROM borrow WHERE book_id is NOT null);
 
8)* 找出李某所借所有图书的书名及借书日期(BORROW_DATE)。
SELECT writer,book_name,
   (SELECT borrow_date FROM borrow WHERE book_id in
      (SELECT book_id from book WHERE writer LIKE '李%'))
FROM book WHERE writer LIKE '李%';
 
9)* 无重复地查询2006年10月以后借书的读者借书证号(READER_ID)、姓名和单位。
SELECT reader_id,name,company FROM reader
WHERE reader_id in
   (SELECT distinct reader_id FROM borrow 
        WHERE borrow_date >= to_date('2006-10-01','yyyy-mm-dd')) ;
 
10)* 找出借阅了<FoxPro大全>一书的借书证号。
SELECT reader_id FROM borrow WHERE book_id in
      (SELECT DISTINCT book_id FROM book
         WHERE book_name='FoxPro大全');
 
11) 找出与”赵正义”在同一天借书的读者姓名、所在单位及借书日期 。
方法一:
SELECT name,company,
      (SELECT borrow_date FROM borrow WHERE reader_id =
      (SELECT reader_id FROM reader WHERE name='赵正义') )  
                  borrow_date
FROM reader WHERE reader_id = (
    SELECT reader_id FROM borrow WHERE  borrow_date =
        (SELECT borrow_date FROM borrow WHERE reader_id =
        (SELECT reader_id FROM reader WHERE name='赵正义') )
    AND reader_id != (SELECT reader_id FROM reader
                                   WHERE name='赵正义') );
 
方法二:联合查询
SELECT reader.name, reader.company, borrow.borrow_date
FROM reader,borrow 
  WHERE reader.reader_id = borrow.reader_id
  AND borrow_date in (
       SELECT borrow_date FROM borrow WHERE reader_id =
          (SELECT reader_id FROM reader WHERE name = '赵正义')
   ) AND reader.name <> '赵正义';
 
12) 查询2006年7月以后没有借书的读者借书证号、姓名及单位。
日期的关系:昨天 > 今天 > 明天
SELECT reader_id,name,company FROM reader
    WHERE reader_id NOT IN
            (SELECT reader_id FROM borrow 
             WHERE borrow_date>=to_date('2006-08-01','yyyy-mm-dd'));
 
13) 求”科学出版社”图书的最高单价、最低单价、平均单价。
SELECT max(price),min(price),avg(price) FROM book 
     WHERE output='科学出版社'
     GROUP BY output;

14)* 求”信息系”当前借阅图书的读者人次数。
SELECT count(distinct reader_id) FROM borrow
     WHERE reader_id in
             (SELECT reader_id FROM reader WHERE company='信息系');

15) 求出各个出版社图书的最高价格、最低价格和总册数。
SELECT max(price),min(price),count(book_id)
      FROM book GROUP BY output;

16) 分别找出各单位当前借阅图书的读者人数及所在单位。
SELECT count(reader_id),company FROM reader 
   WHERE reader_id in (SELECT distinct reader_id FROM borrow)
    GROUP BY company;
 

17)* 找出当前至少借阅了2本图书(大于等于2本)的读者姓名及其所在单位。
SELECT name,company FROM reader WHERE reader_id in

       (SELECT distinct reader_id FROM borrow 
         GROUP BY reader_id having count(book_id)>=2)

;

 
18) 分别找出借书人次数多于1人次的单位及人次数。
SELECT company,count(reader_id) FROM reader 
     WHERE reader_id in(SELECT distinct reader_id FROM borrow)GROUP BY company having count(reader_id)>1;
 
19) 找出藏书中各个出版单位的名称、每个出版社的书籍的总册数(每种可能有多册)、书的价值总额。
SELECT output,count(book_id),sum(price) 
FROM book GROUP BY output;
 
20) 查询经济系是否还清所有图书。如果已经还清,显示该系所有读者的姓名、所在单位和职称。
SELECT name,company,grade FROM reader
  WHERE reader_id not in (SELECT DISTINCT reader_id FROM borrow)
  AND company='经济系';
 
附录:建表语句
创建图书管理库的图书、读者和借阅三个基本表的表结构:
创建BOOK:(图书表)
 CREATE TABLE BOOK (
 BOOK_ID NUMBER(10),
 SORT VARCHAR2(10),
 BOOK_NAME VARCHAR2(50),
 WRITER VARCHAR2(10),
 OUTPUT VARCHAR2(50),
 PRICE NUMBER(3));
 
创建READER:(读者表)
CREATE TABLE READER (
READER_ID NUMBER(3),
COMPANY VARCHAR2(10),
NAME VARCHAR2(10),
SEX VARCHAR2(2),
GRADE VARCHAR2(10),
ADDR VARCHAR2(50));
 
创建BORROW:(借阅表)
CREATE  TABLE  BORROW (
 READER_ID  NUMBER(3),
 BOOK_ID  NUMBER(10),
 BORROW_DATE  DATE);

插入数据:
BOOK表:
insert into book values(445501,'TP3/12','数据库导论','王强','科学出版社',17.90);
insert into book values(445502,'TP3/12','数据库导论','王强','科学出版社',17.90);
insert into book values(445503,'TP3/12','数据库导论','王强','科学出版社',17.90);
insert into book values(332211,'TP5/10','计算机基础','李伟','高等教育出版社',18.00);
insert into book values(112266,'TP3/12','FoxBASE','张三','电子工业出版社',23.60);
insert into book values(665544,'TS7/21','高等数学','刘明','高等教育出版社',20.00);
insert into book values(114455,'TR9/12','线性代数','孙业','北京大学出版社',20.80);
insert into book values(113388,'TR7/90','大学英语','胡玲','清华大学出版社',12.50);
insert into book values(446601,'TP4/13','数据库基础','马凌云','人民邮电出版社',22.50);
insert into book values(446602,'TP4/13','数据库基础','马凌云','人民邮电出版社',22.50);
insert into book values(446603,'TP4/13','数据库基础','马凌云','人民邮电出版社',22.50);
insert into book values(449901,'TP4/14','FoxPro大全','周虹','科学出版社',32.70);
insert into book values(449902,'TP4/14','FoxPro大全','周虹','科学出版社',32.70);
insert into book values(118801,'TP4/15','计算机网络','黄力钧','高等教育出版社',21.80);
insert into book values(118802,'TP4/15','计算机网络','黄力钧','高等教育出版社',21.80);

READER表:
insert into reader values(111,'信息系','王维利','女','教授','1号楼424');
insert into reader values(112,'财会系','李  立','男','副教授','2号楼316');
insert into reader values(113,'经济系','张  三','男','讲师','3号楼105');
insert into reader values(114,'信息系','周华发','男','讲师','1号楼316');
insert into reader values(115,'信息系','赵正义','男','工程师','1号楼224');
insert into reader values(116,'信息系','李  明','男','副教授','1号楼318');
insert into reader values(117,'计算机系','李小峰','男','助教','1号楼214');
insert into reader values(118,'计算机系','许鹏飞','男','助工','1号楼216');
insert into reader values(119,'计算机系','刘大龙','男','教授','1号楼318');
insert into reader values(120,'国际贸易','李  雪','男','副教授','4号楼506');
insert into reader values(121,'国际贸易','李  爽','女','讲师','4号楼510');
insert into reader values(122,'国际贸易','王  纯','女','讲师','4号楼512');
insert into reader values(123,'财会系','沈小霞','女','助教','2号楼202');
insert into reader values(124,'财会系','朱  海','男','讲师','2号楼210');
insert into reader values(125,'财会系','马英明','男','副教授','2号楼212');

BORROW表:
insert into borrow values(112,445501,'19-3月-2006');
insert into borrow values(125,332211,'12-2月-2006');
insert into borrow values(111,445503,'21-8月-2006');
insert into borrow values(112,112266,'14-3月-2006');
insert into borrow values(114,665544,'21-10月-2006');
insert into borrow values(120,114455,'02-11月-2006');
insert into borrow values(120,118801,'18-10月-2006');
insert into borrow values(119,446603,'12-11月-2006');
insert into borrow values(112,449901,'23-10月-2006');
insert into borrow values(115,449902,'21-8月-2006');
insert into borrow values(118,118801,'10-9月-2006');