Skip to content

Latest commit

Β 

History

History
256 lines (194 loc) Β· 8.23 KB

File metadata and controls

256 lines (194 loc) Β· 8.23 KB

Header

Typing SVG


Status MySQL Workbench Queries


LinkedIn GitHub


πŸ›’ About This Project

project = {
    "name"      : "E-Commerce SQL Analysis",
    "database"  : "MySQL β€” Star Schema Design",
    "queries"   : 7,
    "concepts"  : ["JOINs", "CTEs", "Window Functions", "CASE WHEN",
                   "GROUP BY", "Aggregate Functions", "Date Functions"],
    "data"      : "100% Synthetic β€” generated using SQL itself",
    "tables"    : ["customers", "products", "orders", "order_items"],
    "records"   : "1,000 customers | 5,000 orders | 8,000 order items",
    "outcome"   : "πŸ“Š 7 CEO-level business insights extracted from raw data"
}

πŸ’‘ "I built an entire E-Commerce business from scratch β€” just to ask it 7 questions."


πŸ“Š Dataset β€” Self Created Synthetic Data

πŸ“¦ Products πŸ‘₯ Customers πŸ›’ Orders 🧾 Order Items
10 Products 1,000 Customers 5,000 Orders 8,000+ Line Items

⚑ All data was synthetically generated using SQL itself β€” no external CSV or dataset used!


πŸ—„οΈ Database Schema

customers ──< orders ──< order_items >── products
Table Description Key Columns
customers Customer information customer_id, name, email, country
products Product catalog product_id, name, category, price
orders Order transactions order_id, customer_id, date, status
order_items Line items per order item_id, order_id, product_id, qty

πŸ’Ό 7 Business Queries & Insights

Query 1 β€” Total Revenue Generated

SELECT 
    SUM(p.price * oi.quantity) AS total_revenue
FROM order_items oi
JOIN products p ON oi.product_id = p.product_id;

πŸ’‘ Tracks overall financial performance β€” helps management evaluate growth trends and pricing strategy.


Query 2 β€” Top 5 Best Selling Products

SELECT 
    p.product_name,
    SUM(oi.quantity) AS total_units_sold
FROM order_items oi
JOIN products p ON oi.product_id = p.product_id
GROUP BY p.product_name
ORDER BY total_units_sold DESC
LIMIT 5;

πŸ’‘ Identifies top performers β€” helps focus inventory planning, marketing, and supply chain optimization.


Query 3 β€” Top 10 High Value Customers

SELECT 
    c.customer_name,
    SUM(p.price * oi.quantity) AS total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN order_items oi ON o.order_id = oi.order_id
JOIN products p ON oi.product_id = p.product_id
GROUP BY c.customer_name
ORDER BY total_spent DESC
LIMIT 10;

πŸ’‘ Pareto Principle β€” top 20% customers drive 80% revenue. Target these for loyalty programs.


Query 4 β€” Monthly Revenue Trend

SELECT 
    DATE_FORMAT(o.order_date,'%Y-%m') AS month,
    SUM(p.price * oi.quantity) AS revenue
FROM orders o
JOIN order_items oi ON o.order_id = oi.order_id
JOIN products p ON oi.product_id = p.product_id
GROUP BY month
ORDER BY month;

πŸ’‘ Reveals seasonality patterns β€” helps plan promotions during slow months and stock up for peaks.


Query 5 β€” Customer Segmentation (CASE WHEN)

SELECT 
    c.customer_name,
    SUM(p.price * oi.quantity) AS total_spent,
    CASE 
        WHEN SUM(p.price * oi.quantity) > 500    THEN 'High Value'
        WHEN SUM(p.price * oi.quantity) >= 200   THEN 'Medium Value'
        ELSE 'Low Value'
    END AS customer_segment
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN order_items oi ON o.order_id = oi.order_id
JOIN products p ON oi.product_id = p.product_id
GROUP BY c.customer_id, c.customer_name;

πŸ’‘ Enables personalized marketing β€” different campaigns for High, Medium, and Low value segments.


Query 6 β€” Customer Lifetime Value (CTE)

WITH customer_spending AS (
    SELECT 
        c.customer_id,
        c.customer_name,
        SUM(p.price * oi.quantity) AS lifetime_value
    FROM customers c
    JOIN orders o ON c.customer_id = o.customer_id
    JOIN order_items oi ON o.order_id = oi.order_id
    JOIN products p ON oi.product_id = p.product_id
    GROUP BY c.customer_id, c.customer_name
)
SELECT * FROM customer_spending
ORDER BY lifetime_value DESC;

πŸ’‘ High CLV customers deserve retention strategies, premium service, and loyalty rewards.


Query 7 β€” Product Revenue Ranking (Window Function)

SELECT 
    p.product_name,
    SUM(p.price * oi.quantity) AS total_revenue,
    RANK() OVER (
        ORDER BY SUM(p.price * oi.quantity) DESC
    ) AS product_rank
FROM products p
JOIN order_items oi ON p.product_id = oi.product_id
GROUP BY p.product_id, p.product_name;

πŸ’‘ Rankings help prioritize inventory investment and identify which products deserve prime shelf space.


🧠 SQL Concepts Used

Concept Used In
βœ… DDL β€” CREATE DATABASE, TABLE Database Setup
βœ… DML β€” INSERT, SELECT Data Operations
βœ… JOINS β€” Multi-table queries All 7 Queries
βœ… GROUP BY & ORDER BY Queries 1, 2, 3, 4
βœ… CASE WHEN β€” Conditional logic Query 5
βœ… CTE β€” Common Table Expression Query 6
βœ… Window Functions β€” RANK() OVER Query 7
βœ… Aggregate Functions β€” SUM, COUNT Throughout
βœ… DATE Functions β€” DATE_FORMAT Query 4
βœ… Foreign Keys β€” Relational integrity Table Design

πŸ“‚ Project Structure

Ecommerce-SQL-Analysis/
β”‚
└── ecommerce_analysis.sql
    β”œβ”€β”€ Database Creation    β†’ CREATE DATABASE & USE
    β”œβ”€β”€ Table Creation       β†’ 4 tables with foreign keys
    β”œβ”€β”€ Data Insertion       β†’ 1000+ synthetic records
    └── Business Queries     β†’ 7 advanced business queries

πŸ› οΈ Tools & Technologies

MySQL MySQL Workbench SQL


πŸ‘¨β€πŸ’» Author

Devesh Shukla Data Analyst | SQL Developer | Builder

LinkedIn GitHub CHANAKYA


⭐ If you find this useful, please give it a star! ⭐

Footer