Design an Online Bookstore with Its SQL Queries
Model books, authors, carts and orders with a clean order lifecycle, map the model to tables, write three reporting queries, and keep the last copy from being sold twice.
Design the main classes of an online bookstore and their relationships, then turn the model into database tables and write a few SQL queries against it.
For this exercise, assume: - The store sells physical books. A book has a title, an ISBN, a price and a stock count, belongs to one category, and can have several authors; an author can write many books. - A customer has a cart of books with quantities. Checking out turns the cart into an order.
An order has one line per book with the quantity and the unit price at the time of purchase, so later price changes do not change old orders. - An order moves from PLACED to PAID to SHIPPED, or from PLACED or PAID to CANCELLED. Placing an order takes the books out of stock; cancelling puts them back.
An order cannot be placed if any line asks for more copies than are in stock, and then nothing changes. - Payment goes through an outside payment provider; only the order's state change is in scope.
Search ranking, recommendations, reviews, shipping logistics and the high-level system architecture are out of scope here.
The SQL part asks for three queries: the 5 books with the most copies sold in orders placed in the last 30 days, not counting cancelled orders; the total each customer has spent on orders that are not cancelled, highest first; and the books by one given author that are in stock.