laiyenju / YenBlog

My note blog about learning, design and projects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SQL 練習題

laiyenju opened this issue · comments

SQL 練習題

tags: Database SQL

👇 Database 👇

Tables
Authors id name
Books id title author_id publisher_id
Publishers id name
Readers id name department gender
Readerships id reader_id book_id

題目

  1. 總共有幾本書在資料庫中?
select count(*)
from books;

--answer: 100
  1. 總共有幾位作者在資料庫中?
select count(*)
from authors;

--answer: 59
  1. 總共有幾家出版社在資料庫中?
select count(*)
from publishers;

--answer: 33
  1. 一位作者的書只算一次,總共有幾本不同作者的書?
select count(distinct author_id)
from books;

--answer: 59
  1. 平均每位讀者借過幾次書(包含重覆的書)?
select count(*)/count(distinct reader_id)
from readerships;

--answer: 50

==6. 平均每位讀者借過幾本不重覆的書?==

select count(distinct book_id, reader_id)/count(distinct reader_id)
from readerships;

--answer: 39.14
  1. 借閱次數最多的讀者(包含重覆借同一本書),總共借了幾次書?
select r.name, count(rs.book_id)
from readerships as rs
join readers as r
    on r.id = rs.reader_id
group by r.name
order by count(rs.id) desc limit 1;

--answer: 66
  1. 借閱記錄最多的讀者的名字是(包含重覆借同一本書)?
select r.name, count(rs.book_id)
from readerships as rs
join readers as r
    on r.id = rs.reader_id
group by r.name
order by count(rs.id) desc limit 1;

--answer: Cathey Gardella
  1. 借過最多本不同書的讀者,總共借過幾本不同的書?
select r.name, count(distinct rs.book_id)
from readerships as rs
join readers as r
    on r.id = rs.reader_id
group by r.name
order by count(rs.book_id) desc limit 1;

--answer: 47
  1. 借過最多本不同書的讀者,名字是?
select r.name, count(distinct rs.book_id) as cnt
from readers as r
join readerships as rs
    on r.id = rs.reader_id
group by r.name
order by cnt desc limit 1;

--answer: Tashia Piccard
  1. 所有女性讀者借書的總次數是?
select count(rs.id)
from readerships as rs
join readers as r
    on r.id = rs.reader_id
where r.gender = 'F';

--answer: 2570
  1. 已知共有50位男性讀者,所有男性讀者借書的平均次數是?
select count(rs.id)/50
from readerships as rs
join readers as r
    on r.id = rs.reader_id
where r.gender = 'M';

--answer: 48.6000

==13. 哪一本書最熱門?(被最多不同讀者借過)==

注意 group by 要以 books.id 來分組

select b.title, count(distinct rs.reader_id) as cnt
from readerships as rs
join books as b
    on b.id = rs.book_id
group by b.id
order by cnt desc limit 1;

--answer: Harry Potter and the Half-blood Prince

==14. 哪一本書最花時間消化?(每位讀者平均借閱次數最多)==

注意 group by 要以 books.id 來分組

select b.title, count(rs.id)/count(distinct rs.reader_id) as cnt
from readerships as rs
join books as b
    on rs.book_id = b.id
group by b.id
order by cnt desc limit 1;
  1. 哪一位讀者閱讀速度最快(平均每一本書的借閱次數最少)?
select r.name, count(rs.id)/count(distinct rs.book_id) as cnt
from readerships as rs
join readers as r
    on r.id = rs.reader_id
group by r.name
order by cnt asc limit 1;

--answer: Meryl Nguyen
  1. 哪一家出版社出最多書?
select p.name, count(b.publisher_id) as cnt
from books as b
join publishers as p
    on p.id = b.publisher_id
group by p.id
order by cnt desc limit 1;

--answer: Bloomsbury Pub PLC

==17. 哪一家出版社旗下的書,被最多(不重覆)讀者借過?==

以同時具有兩個表格外鍵的「中間表格」,當做第一個出現的表格,後面會較好寫指令。

select p.name, count(distinct rs.reader_id) as cnt
from readerships as rs
join books as b
    on b.id = rs.book_id
join publishers as p
    on p.id = b.publisher_id
group by p.id
order by cnt desc limit 1;

--answer: Bloomsbury Pub PLC
  1. 有些書是匯編而成的,所以沒有列作者。這樣子的書共有幾本?
select count(*)
from books
where author_id is null;

--answer: 4
  1. 最多產(寫過最多本書)的作家是誰?
select a.name, count(b.id)
from books as b
join authors as a
    on a.id = b.author_id
group by a.name
order by count(b.id) desc limit 1;

--answer: Rowling, J. K.
  1. 有幾本書書名中含有 time 這個字?(不區分大小寫)
select count(*)
from books
where title like '%time%';

-- answer: 4