Perfect project for Data, Product, and CRM Analysts. This one hits both SQL fundamentals + advanced customer analytics.
๐ฏ Business Objectives
โ Analyze customer retention
โ Build monthly customer cohorts
โ Measure repeat purchases
โ Calculate customer lifetime value
โ Identify high-value customers
โ Analyze customer churn
โ Compare cohort performance
โ Measure revenue retention
๐ Step 1: Create Database
CREATE DATABASE cohort_analysis_db;
USE cohort_analysis_db;
๐ Step 2: Create Customers Table
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(100),
signup_date DATE,
city VARCHAR(50)
);
๐ Step 3: Create Orders Table
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
order_amount DECIMAL(10,2),
order_status VARCHAR(20),
FOREIGN KEY (customer_id)
REFERENCES customers(customer_id)
);
๐ Step 4: Insert Sample Customers
INSERT INTO customers VALUES
(1,'Rahul Sharma','2025-01-05','Mumbai'),
(2,'Priya Verma','2025-01-10','Delhi'),
(3,'Amit Patel','2025-02-15','Pune'),
(4,'Sneha Joshi','2025-02-20','Bangalore'),
(5,'Rohan Gupta','2025-03-05','Hyderabad'),
(6,'Anjali Shah','2025-03-15','Mumbai');
๐ Step 5: Insert Sample Orders
INSERT INTO orders VALUES
(1001,1,'2025-01-10',2500,'Completed'),
(1002,1,'2025-02-15',3200,'Completed'),
(1003,1,'2025-03-20',1800,'Completed'),
(1004,2,'2025-01-20',1500,'Completed'),
(1005,2,'2025-03-05',2800,'Completed'),
(1006,3,'2025-02-20',4200,'Completed'),
(1007,3,'2025-03-25',3500,'Completed'),
(1008,4,'2025-02-25',1800,'Completed'),
(1009,5,'2025-03-10',2200,'Completed'),
(1010,5,'2025-04-15',3100,'Completed'),
(1011,6,'2025-03-20',2700,'Completed');
๐ง SQL Concepts You'll Practice
โ Date Functions
โ GROUP BY, Aggregate Functions, JOINs
โ CTEs, CASE WHEN, Subqueries
โ Window Functions, LAG(), DENSE_RANK()
โ COUNT DISTINCT, Cohort Analysis
๐ Business KPIs You Can Build
๐ Monthly Active Customers
๐ New Customers by Month
๐ Repeat Customers, Repeat Purchase Rate
๐ Customer Retention Rate, Churn Rate
๐ Customer Lifetime Value (CLV), AOV
๐ Revenue per Cohort, Cohort Retention Rate
๐ RFM Segmentation, High-Value Customer %
๐ก Example Queries
1. Find Customer Cohort
WITH customer_cohort AS (
SELECT
customer_id,
DATE_TRUNC('month', MIN(signup_date)) AS cohort_month
FROM customers
GROUP BY customer_id
)
SELECT * FROM customer_cohort ORDER BY cohort_month;
2. Monthly Customer Activity
SELECT
DATE_TRUNC('month', order_date) AS order_month,
COUNT(DISTINCT customer_id) AS active_customers
FROM orders
WHERE order_status = 'Completed'
GROUP BY DATE_TRUNC('month', order_date)
ORDER BY order_month;
3. Repeat Customers
SELECT
customer_id,
COUNT(order_id) AS total_orders
FROM orders
WHERE order_status = 'Completed'
GROUP BY customer_id
HAVING COUNT(order_id) > 1;
4. Customer Lifetime Value
SELECT
customer_id,
SUM(order_amount) AS lifetime_value
FROM orders
WHERE order_status = 'Completed'
GROUP BY customer_id
ORDER BY lifetime_value DESC;
5. Rank Customers by Revenue
WITH customer_revenue AS (
SELECT customer_id, SUM(order_amount) AS revenue
FROM orders WHERE order_status = 'Completed' GROUP BY customer_id
)
SELECT
customer_id,
revenue,
DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank
FROM customer_revenue;