0% found this document useful (0 votes)
21 views4 pages

As 4

The document describes the schema for an online bookstore database including tables for books, authors, customers, orders, and order items. It also includes example queries that can be run on the database such as listing books and their authors, finding the best selling books, and calculating customer revenue and orders placed. The schema and queries allow the bookstore to track inventory, sales, customers and perform analytics on orders and purchases.

Uploaded by

abhinavreddy3057
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

As 4

The document describes the schema for an online bookstore database including tables for books, authors, customers, orders, and order items. It also includes example queries that can be run on the database such as listing books and their authors, finding the best selling books, and calculating customer revenue and orders placed. The schema and queries allow the bookstore to track inventory, sales, customers and perform analytics on orders and purchases.

Uploaded by

abhinavreddy3057
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Book Table:

Fields: ISBN (Primary Key), Title, AuthorID (Foreign Key), Genre, Price

Constraints: ISBN (Primary Key), Title (NOT NULL), Price (CHECK Price >= 0)

CREATE TABLE Book (


ISBN INT PRIMARY KEY,
Title VARCHAR(255) NOT NULL,
AuthorID INT,
Genre VARCHAR(255),
Price DECIMAL(10, 2) CHECK (Price >= 0),
FOREIGN KEY (AuthorID) REFERENCES Author(AuthorID)
);

Author Table:
Fields: AuthorID (Primary Key), Name, BirthDate

Constraints: AuthorID (Primary Key), Name (NOT NULL)

CREATE TABLE Author (


AuthorID INT PRIMARY KEY,
Name VARCHAR(255) NOT NULL
);

Customer Table:
Fields: CustomerID (Primary Key), Name, Email, Address

Constraints: CustomerID (Primary Key), Name (NOT NULL), Email (NOT NULL)

CREATE TABLE Customer (


CustomerID INT PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
Email VARCHAR(255) NOT NULL,
Address VARCHAR(255)
);
Order Table:
Fields: OrderID (Primary Key), CustomerID (Foreign Key), OrderDate

Constraints: OrderID (Primary Key), CustomerID (Foreign Key), OrderDate (NOT NULL)

CREATE TABLE Order (


OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE NOT NULL,
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
);

Order_Item Table:
Fields: OrderItemID (Primary Key), OrderID (Foreign Key), BookID (Foreign Key),
Quantity

Constraints: OrderItemID (Primary Key), OrderID (Foreign Key), BookID (Foreign


Key), Quantity (CHECK Quantity > 0)

CREATE TABLE Order_Item (


OrderItemID INT PRIMARY KEY,
OrderID INT,
BookID INT,
Quantity INT CHECK (Quantity > 0),
FOREIGN KEY (OrderID) REFERENCES Order(OrderID),
FOREIGN KEY (BookID) REFERENCES Book(ISBN)
);

Queries:

List all books with their authors

SELECT b.ISBN, b.Title, a.Name AS Author


FROM Book b
JOIN Author a ON b.AuthorID = a.AuthorID;

Display the total number of orders placed by each customer

SELECT c.CustomerID, c.Name, COUNT(o.OrderID) AS TotalOrders


FROM Customer c
JOIN Order o ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerID, c.Name;
Find the top 5 best-selling books

SELECT b.ISBN, b.Title, COUNT(oi.OrderItemID) AS TotalSales


FROM Book b
JOIN Order_Item oi ON b.ISBN = oi.BookID
GROUP BY b.ISBN, b.Title
ORDER BY TotalSales DESC
LIMIT 5;

Show the average price of books in each genre

SELECT b.Genre, AVG(b.Price) AS AveragePrice


FROM Book b
GROUP BY b.Genre;

List customers who have ordered books written by a specific author

SELECT c.CustomerID, c.Name


FROM Customer c
JOIN Order o ON c.CustomerID = o.CustomerID
JOIN Order_Item oi ON o.OrderID = oi.OrderID
JOIN Book b ON oi.BookID = b.ISBN
WHERE b.AuthorID = ?;

Display the total revenue generated by each customer

SELECT c.CustomerID, c.Name, SUM(oi.Quantity * b.Price) AS TotalRevenue


FROM Customer c
JOIN Order o ON c.CustomerID = o.CustomerID
JOIN Order_Item oi ON o.OrderID = oi.OrderID
JOIN Book b ON oi.BookID = b.ISBN
GROUP BY c.CustomerID, c.Name;

Find the customers who have not placed any orders

SELECT c.CustomerID, c.Name


FROM Customer c
LEFT JOIN Order o ON c.CustomerID = o.CustomerID
WHERE o.OrderID IS NULL;

List the books that have never been ordered

SELECT b.ISBN, b.Title


FROM Book b
LEFT JOIN Order_Item oi ON b.ISBN = oi.BookID
WHERE oi.OrderItemID IS NULL;
Show the number of orders placed each month

SELECT MONTH(o.OrderDate) AS Month, COUNT(o.OrderID) AS TotalOrders


FROM Order o
GROUP BY MONTH(o.OrderDate);

Display the oldest and newest books in the database

SELECT b.ISBN, b.Title, b.Genre, a.Name AS Author, MIN(b.Price) AS OldestBook,


MAX(b.Price) AS NewestBook
FROM Book b
JOIN Author a ON b.AuthorID = a.AuthorID;

You might also like