A beginner-to-intermediate SQL project built using PostgreSQL to analyze an online book store database. The project covers database design, data import, and solving real-world business queries ranging from basic to advanced.
The project uses 3 tables connected via common key columns:
| Column | Type |
|---|---|
| Book_ID | SERIAL PRIMARY KEY |
| Title | VARCHAR(100) |
| Author | VARCHAR(100) |
| Genre | VARCHAR(50) |
| Published_Year | INT |
| Price | NUMERIC(10,2) |
| Stock | INT |
| Column | Type |
|---|---|
| Customer_ID | SERIAL PRIMARY KEY |
| Name | VARCHAR(100) |
| VARCHAR(100) | |
| Phone | VARCHAR(15) |
| City | VARCHAR(50) |
| Country | VARCHAR(150) |
| Column | Type |
|---|---|
| Order_ID | SERIAL PRIMARY KEY |
| Customer_ID | INT (FK → Customers) |
| Book_ID | INT (FK → Books) |
| Order_Date | DATE |
| Quantity | INT |
| Total_Amount | NUMERIC(10,2) |
📦 Online-Book-Store-SQL
┣ 📄 Online_Book_Store_Analysis.sql # All SQL queries (DDL + DML + Analysis)
┣ 📊 Books.csv # Books dataset
┣ 📊 Customers.csv # Customers dataset
┣ 📊 Orders.csv # Orders dataset
┗ 📄 README.md
- Retrieve all books in the "Fiction" genre
- Find books published after the year 1950
- List all customers from Canada
- Show orders placed in November 2023
- Retrieve the total stock of books available
- Find the details of the most expensive book
- Show all customers who ordered more than 1 quantity of a book
- Retrieve all orders where the total amount exceeds $20
- List all distinct genres available in the Books table
- Find the book with the lowest stock
- Calculate the total revenue generated from all orders
- Total number of books sold per genre
- Average price of books in the "Fantasy" genre
- Customers who have placed at least 2 orders
- The most frequently ordered book
- Top 3 most expensive books in the 'Fantasy' genre
- Total quantity of books sold by each author
- Cities where customers who spent over $30 are located
- The customer who spent the most on orders
- Database: PostgreSQL
- Language: SQL
- Concepts Used: DDL, DML, Aggregate Functions, JOINs, GROUP BY, HAVING, Subqueries, ORDER BY, LIMIT
- Open pgAdmin or any PostgreSQL client (e.g., psql, DBeaver)
- Create a new database:
CREATE DATABASE online_book_store;
- Run the
Online_Book_Store_Analysis.sqlfile to create tables - Update the file paths in the
COPYcommands to match your local CSV file locations:COPY Books FROM 'your/local/path/Books.csv' CSV HEADER;
- Execute the queries one by one or all at once to explore the results
- Designing a relational database with primary and foreign key constraints
- Using
JOINto connect multiple tables for meaningful insights - Applying aggregate functions (
SUM,AVG,COUNT) withGROUP BYandHAVING - Filtering data with
WHERE,BETWEEN, andDISTINCT - Writing subqueries to calculate derived metrics like remaining stock
Feel free to fork this project, raise issues, or suggest improvements. Happy querying! 🎯