PPT - Clinton McKay

advertisement
SQL Database for a
Book Store
Clinton McKay
Explanation
The database contains information about the books
held in stock, their authors, publishers, customers,
sales. The database was populated with information
from bibliographic records in IUCat (with some
modifications) and do not necessarily represent
actual publications. The SQL queries I designed are
meant to gather different kinds of useful information
about the business.
Entity Relationship Diagram
Data Dictionary
Query 1
List all titles in “book” and include
ISBN, author name (as combined
from author.fname and
author.lname) and the publisher
name.
SELECT
book.title AS "Title", book.isbn AS
"ISBN", CONCAT(author.fname, ' ',
author.lname) AS "Author",
publisher.name AS "Publisher"
FROM book, author, publisher
WHERE
author.author_id=book.author_id
AND
publisher.publisher_id=book.publis
her_id;
Query 2
List all customers who have
purchased books published since
2007.
SELECT customer.fname,
customer.lname, book.title,
book.publication_date
FROM customer
JOIN sale ON
customer.customer_id=sale.cust_i
d
JOIN book ON sale.isbn=book.isbn
JOIN author ON
author.author_id=book.author_id
WHERE book.publication_date >=
2007;
Query 3
List customers (as combined from
customer.fname and customer.lname) who
have purchased books published in the UK
or the US, as well as the title of the book
they purchased and the name of its
publisher and order by last name of
customer.
SELECT CONCAT(customer.fname, ' ',
customer.lname), book.title,
publisher.country, publisher.name
FROM customer
JOIN sale ON customer.customer_id =
sale.cust_id
JOIN book ON sale.isbn = book.isbn
JOIN publisher ON book.publisher_id =
publisher.publisher_id
WHERE publisher.country ='UK' or
publisher.country ='US'
ORDER BY customer.lname;
Query 4
List the number of books sold that
have been written by each author
and group by author’s first name,
then last name.
SELECT COUNT(sale.isbn) AS
"Number of books sold",
CONCAT(author.fname, ' ',
author.lname) AS "Author"
FROM sale
JOIN book ON sale.isbn =
book.isbn
RIGHT JOIN author ON
book.author_id=author.author_id
GROUP BY author.fname,
author.lname;
Query 5
List the different (distinct) genres and how
many books belong to each genre, order
alphabetically by genre.
SELECT distinct book.genre,
COUNT(book.genre)
FROM book
GROUP BY book.genre
ORDER BY book.genre ASC;
Query 6
List the names and phone numbers of all distinct
customers who have ordered books published by US or
UK publishers, sort them alphabetically.
SELECT DISTINCT (CONCAT (customer.fname, ' ',
customer.lname)) AS "Customer Name",
customer.phone AS "Customer Phone Number"
FROM customer
JOIN sale ON sale.cust_id = customer.customer_id
JOIN book ON book.isbn = sale.isbn
WHERE (CONCAT (customer.fname, ' ',
customer.lname)) IN (SELECT CONCAT(customer.fname,
' ', customer.lname)
FROM customer
JOIN sale ON customer.customer_id = sale.cust_id
JOIN book ON sale.isbn = book.isbn
JOIN publisher ON book.publisher_id =
publisher.publisher_id
WHERE publisher.country ='UK' or publisher.country
='US')
ORDER BY CONCAT(customer.fname, ' ',
customer.lname);
Query 7
List customers’ first and last names ordered by
the number of books they have purchased,
ordered by the number of books purchased.
SELECT COUNT(sale.cust_id) AS "Number of
Books Purchased",
CONCAT (customer.fname, ' ', customer.lname)
AS "Customer Name"
FROM sale
JOIN customer ON customer.customer_id =
sale.cust_id
GROUP BY customer.fname, customer.lname
ORDER BY (COUNT(sale.cust_id)) DESC;
Query 8
List the number of records in each table (book,
author, sale, customer, and publisher).
SELECT
DISTINCT (SELECT COUNT(*) FROM book) AS
"Books",
(SELECT COUNT(*) FROM author) AS
"Authors",
(SELECT COUNT(*) FROM sale) AS "Sales",
(SELECT COUNT(*) FROM customer) AS
"Customers",
(SELECT COUNT(*) FROM publisher) AS
"Publishers"
FROM book
JOIN author ON author.author_id =
book.author_id
JOIN sale ON sale.isbn = book.isbn
JOIN customer ON
customer.customer_id=sale.cust_id
JOIN publisher ON publisher.publisher_id =
book.publisher_id;
Query 9
Create and display a view of all sales
with customer name, title of book,
author last name, isbn of book, and
publisher name.
CREATE VIEW listsales AS
SELECT CONCAT(customer.fname, ' ',
customer.lname) AS "Customer
Name", book.title AS "Book",
author.lname AS "Author Last Name",
book.isbn AS "ISBN", publisher.name
AS "Publisher"
FROM customer
JOIN sale ON customer.customer_id =
sale.cust_id
JOIN book ON sale.isbn = book.isbn
JOIN author ON author.author_id =
book.author_id
JOIN publisher ON book.publisher_id
= publisher.publisher_id
ORDER BY customer.lname;
SELECT * FROM listsales;
Query 10
Books are organized by ISBN—Books
with ISBNs greater than 700000000 are
housed on the third floor, those with
ISBNs between 3000000000 and
700000000 are housed on the second
floor, and all others are on the first
floor. List all books with their locations.
SELECT book.title AS "Title", CASE
WHEN book.isbn >= 700000000 THEN
'Located on Third Floor'
WHEN book.isbn <= 300000000 THEN
'Located on First Floor'
ELSE 'Located on Second Floor'
END AS "Location"
FROM book;
Download