Identify key drivers of customer retention, churn, and revenue using normalized raw database tables.
This project uses PostgreSQL 17.
- End to ennd SQL analysis on customer and transaction data
- Cohort analysis to track retention over time
- Customer segmentation using RFM logic
- Revenue trend and growth analysis
- Retention and churn identification
- Support impact analysis on customer behavior
- Reusable reporting views for business insights
I engineered a series of analytical pipelines using PostgreSQL 17 that automate:
- Revenue Growth Modeling: Calculation of MoM growth and category-specific performance.
- Cohort Retention: Matrix-style analysis tracking users from their acquisition month.
- RFM Segmentation: (Recency, Frequency, Monetary) logic to rank customer value.
- Churn Diagnostics: Identifying dormant 90+ day users and the "single-order" drop-off.
- Support Sentiment Analysis: Correlating ticket resolution times with long-term LTV.
- CTEs & Window Functions: For running totals, MoM growth percentages, and ranking.
- Self-Joins: Used specifically to calculate time-to-next-purchase intervals.
- Complex Aggregations: Pivot-style queries for cohort heatmaps.
- Materialized View Logic: To optimize reporting for business users.
- Open pgAdmin.
- Create a new database:
CREATE DATABASE customer_retention_case_study;- Connect to that database.
- Open each SQL file and run in this exact order:
schema.sql
insert_sample_data.sql
views_for_reporting.sql
revenue_analysis.sql
retention_analysis.sql
cohort_analysis.sql
customer_segmentation.sql
support_impact_analysis.sql
createdb customer_retention_case_studyThen run:
psql -d customer_retention_case_study -f sql/schema.sql
psql -d customer_retention_case_study -f sql/insert_sample_data.sql
psql -d customer_retention_case_study -f sql/views_for_reporting.sql
psql -d customer_retention_case_study -f sql/revenue_analysis.sql
psql -d customer_retention_case_study -f sql/retention_analysis.sql
psql -d customer_retention_case_study -f sql/cohort_analysis.sql
psql -d customer_retention_case_study -f sql/customer_segmentation.sql
psql -d customer_retention_case_study -f sql/support_impact_analysis.sqlIf using a username:
psql -U postgres -d customer_retention_case_study -f sql/schema.sqlAfter running queries, export important result tables from pgAdmin as CSV files.
- What is the monthly revenue trend?
- Which acquisition channels generate the most revenue?
- Who are the top customers by lifetime value?
- Which product categories drive the highest revenue?
- What is month-over-month revenue growth?
- Which locations generate the most revenue?
- What percentage of customers return after first purchase?
- What is monthly customer retention?
- Which acquisition channel has the best repeat purchase rate?
- How many customers churn after one order?
- Which customers are dormant for 90+ days?
- What is the average time between purchases?
- What does cohort activity look like by first purchase month?
- What is cohort retention percentage?
- What are 30/60/90-day retention rates?
- What are customer RFM segments?
- Which customers are high, medium, or low value?
- Who are loyal repeat customers?
- Which product categories are preferred by each segment?
- Do unresolved support tickets hurt retention or revenue?
- Does satisfaction score affect repeat purchase behavior?
- Which issue types are linked to poor satisfaction?
I generated a schema diagram using: dbdiagram.io
ERD relationship structure:
customers 1---many orders
orders 1---many order_items
products 1---many order_items
orders 1---1 payments
customers 1---many customer_activity
customers 1---many support_tickets