3.1 查询2019年出生的读者姓名、工作单位、身份证号
SELECT readerName,workUnit,identifycard FROM reader WHERE identifycard LIKE '%1991%';
3.2查询图书名中含有“数据库”的图书的详细信息
SELECT * FROM book WHERE bookName LIKE '%数据库%';
3.3查询在2015——2016年之间入库的图书编号、出版时间、入库时间和图书名称,并按入库时间的降序排序输出
SELECT bookNo,publishingDate,shopDate,bookName FROM book
WHERE year(shopDate) IN (2015,2016) ORDER BY shopDate DESC;
3.4查询读者“喻自强”借阅的图书编号、图书名称、借书日期和归还日期
SELECT a.bookNo,bookName,borrowDate,returnDate FROM borrow a,book b,reader c
WHERE a.bookNo=b.bookNo AND a.readerNo=c.readerNo AND readerName='喻自强';
3.5查询借阅了清华大学出版社出版的图书的读者编号、读者姓名、图书名称、借书日期和归还日期
SELECT a.readerNo,readerName,bookName,borrowDate,returnDate FROM reader a,borrow b,book c,publisher d
WHERE a.readerNo=b.readerNo AND b.bookNo=c.bookNo AND c.publisherNo=d.publisherNo
AND publisherName='清华大学出版社';
3.6查询会计学院没有归还所借图书的读者编号、读者姓名、图书名称、借书日期和应归还日期
SELECT a.readerNo,readerName,bookName,borrowDate,shouldDate FROM reader a,borrow b,book c
WHERE a.readerNo=b.readerNo AND b.bookNo=c.bookNo AND returnDate IS NULL;
3.7查询在2015—2016年之间借阅但还未归还图书的读者编号、读者姓名、以及这些借阅为归还图书的图书编号、图书名称和借书日期
SELECT a.readerNo,readerName,c.bookNo,bookName,borrowDate FROM reader a,borrow b,book c
WHERE a.readerNo=b.readerNo AND b.bookNo=c.bookNo AND year(borrowDate) IN (2015,2016) AND returnDate IS NULL;
3.8查询每种类别图书的分类号、分类名称、最高价格和平均价格,并按最高价格的升序输出
SELECT a.classNo,className,max(price),avg(price) FROM book a,bookclass b
WHERE a.classNo=b.classNo GROUP BY classNo ORDER BY max(price);
3.9查询每个读者在借(即借阅未完归还)的图书数量、读者编号、读者姓名、工作单位,并按借书数量的降序排序输出
SELECT count(bookNo),a.readerNo,readerName,workUnit FROM reader a,borrow b
WHERE a.readerNo=b.readerNo GROUP BY bookNo ORDER BY count(bookNo) DESC;
3.10查询每个出版社出版的每种类别的图书的平均价格,要求显示出版社名称、图书类别名称和平均价格
SELECT publisherName,className,avg(price) FROM book a,bookclass b,publisher c
WHERE a.classNo=b.classNo AND a.publisherNo=c.publisherNo GROUP BY publisherName,className;
3.11查询在借图书的总价不低于200元的读者编号、读者姓名和在借图书总价
SELECT a.readerNo,readerName,sum(price) FROM reader a,borrow b,book c
WHERE a.readerNo=b.readerNo AND b.bookNo=c.bookNo AND returnDate IS NULL
GROUP BY readerNo HAVING sum(price)>=200;
3.12查询从来没有借过书的读者姓名和工作单位
使用IN子查询:
SELECT readerName,workUnit FROM reader
WHERE readerNo NOT IN (SELECT readerNo FROM borrow);
使用存在量词子查询:
SELECT readerName,workUnit FROM reader x
WHERE NOT EXISTS (SELECT * FROM borrow a WHERE x.readerNo=a.readerNo);
3.13查询目前没有在借“经济类”图书的读者编号、读者姓名和出生日期
IN子查询:
SELECT readerNo,readerName,SUBSTR(identifycard FROM 7 FOR 8) 出生日期 FROM reader
WHERE readerNo NOT IN
(SELECT readerNo FROM borrow WHERE bookNo IN
(SELECT bookNo FROM book WHERE classNo IN
(SELECT classNo FROM bookclass WHERE className LIKE '%经济类%')));
存在量词子查询:
SELECT readerNo,readerName,SUBSTR(identifycard FROM 7 FOR 8) 出生日期 FROM reader x
WHERE NOT EXISTS
(SELECT * FROM borrow a,book b,bookclass c
WHERE x.readerNo=a.readerNo AND a.bookNo=b.bookNo AND b.classNo=c.classNo
AND className LIKE '%经济类%');
3.14查询既借阅过“政治经济学”图书又借阅过“数据库系统概念”图书的读者编号、读者姓名以及这两种图书的图书名称、借书日期和归还日期
SELECT a.readerNo,readerName,bookName,borrowDate,returnDate FROM reader a,borrow b,book c
WHERE a.readerNo=b.readerNo AND b.bookNo=c.bookNo AND bookName IN ('政治经济学','数据库系统概念')
AND a.readerNo IN
(SELECT readerNo FROM borrow,book
WHERE borrow.bookNo=book.bookNo AND bookName='政治经济学' AND readerNo IN
(SELECT readerNo FROM borrow,book WHERE borrow.bookNo=book.bookNo AND bookName='数据库系统概念'));
3.15查询借阅过图书名称中包含“数据库”的所有图书的读者编号、读者姓名以及他们所借阅的这些图书的图书名称、借阅日期和归还日期
SELECT a.readerNo,readerName,bookName,borrowDate,returnDate FROM reader a,borrow b,book c
WHERE a.readerNo=b.readerNo AND b.bookNo=c.bookNo AND bookName LIKE '%数据库%';
3.16查询至少借阅过读者“张小娟”所借阅过的所有图书的读者编号、读者姓名和工作单位
SELECT a.readerNo,readerName,workUnit FROM reader a RIGHT JOIN borrow b ON a.readerNo=b.readerNo
WHERE bookNo IN
(SELECT bookNo FROM reader a,borrow b WHERE a.readerNo=b.readerNo AND readerName='张小娟')
GROUP BY readerNo
HAVING count(DISTINCT bookNo)>=3;
3.17查询至少有三本在借图书的读者编号、读者姓名以及在借图书的图书编号、图书名称,按读者编号升序、借阅日期降序排序输出
SELECT a.readerNo,readerName,c.bookNo,bookName FROM reader a,borrow b,book c
WHERE a.readerNo=b.readerNo AND b.bookNo=c.bookNo AND returnDate IS NULL
GROUP BY readerNo HAVING count(b.bookNo)>=3;