1 / 0

SQL Database for a Book Store

SQL Database for a Book Store. Clinton McKay. Explanation.

vina
Download Presentation

SQL Database for a Book Store

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SQL Database for a Book Store

    Clinton McKay
  2. 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.
  3. Entity Relationship Diagram
  4. Data Dictionary
  5. Query 1List 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.isbnAS "ISBN", CONCAT(author.fname, ' ', author.lname) AS "Author", publisher.name AS "Publisher" FROM book, author, publisherWHEREauthor.author_id=book.author_id AND publisher.publisher_id=book.publisher_id;
  6. Query 2List all customers who have purchased books published since 2007.SELECT customer.fname, customer.lname, book.title, book.publication_dateFROM customerJOIN sale ON customer.customer_id=sale.cust_idJOIN book ON sale.isbn=book.isbnJOIN author ON author.author_id=book.author_idWHERE book.publication_date >= 2007;
  7. Query 3List 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;
  8. Query 4List 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 saleJOIN book ON sale.isbn = book.isbnRIGHT JOIN author ONbook.author_id=author.author_idGROUP BY author.fname, author.lname;
  9. Query 5List 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.genreORDER BY book.genre ASC;
  10. Query 6List 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 customerJOIN sale ON sale.cust_id = customer.customer_idJOIN book ON book.isbn = sale.isbnWHERE (CONCAT (customer.fname, ' ', customer.lname)) IN (SELECT CONCAT(customer.fname, ' ', customer.lname)FROM customerJOIN sale ON customer.customer_id = sale.cust_idJOIN book ON sale.isbn = book.isbnJOIN publisher ON book.publisher_id = publisher.publisher_idWHERE publisher.country ='UK' or publisher.country ='US')ORDER BY CONCAT(customer.fname, ' ', customer.lname);
  11. Query 7List 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_idGROUP BY customer.fname, customer.lnameORDER BY (COUNT(sale.cust_id)) DESC;
  12. Query 8List 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_idJOIN sale ON sale.isbn = book.isbnJOIN customer ON customer.customer_id=sale.cust_idJOIN publisher ON publisher.publisher_id = book.publisher_id;
  13. Query 9Create 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 ASSELECT 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 customerJOIN sale ON customer.customer_id = sale.cust_idJOIN book ON sale.isbn = book.isbnJOIN author ON author.author_id = book.author_idJOIN publisher ON book.publisher_id = publisher.publisher_idORDER BY customer.lname; SELECT * FROM listsales;
  14. Query 10Books 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;
More Related