TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2652 1.89K
๐Ÿš€ SQL Project Series #30: E-Commerce Customer Retention & Cohort Analysis ๐Ÿ“Š

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;
  • โค 4
  • ๐Ÿ‘ 1
More from @sqlanalyst
  1. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  2. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  3. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  4. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  5. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  6. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’