Question 1: Calculate conversion rate for a sales funnel
Tables: events (user_id, event_name, event_date)
Events: signup → product_view → purchase
Requirement: Count users at each stage, compute conversion from signup to purchase
WITH stages AS (
SELECT user_id,
MAX(CASE WHEN event_name = 'signup' THEN 1 ELSE 0 END) AS signup,
MAX(CASE WHEN event_name = 'product_view' THEN 1 ELSE 0 END) AS view,
MAX(CASE WHEN event_name = 'purchase' THEN 1 ELSE 0 END) AS purchase
FROM events
GROUP BY user_id
)
SELECT
SUM(signup) AS signups,
SUM(purchase) AS purchases,
ROUND(100.0 * SUM(purchase) / SUM(signup), 2) AS conversion_rate
FROM stages;
Question 2: Build a monthly retention cohort
Tables: users (user_id, signup_date), logins (user_id, login_date)
Requirement: Cohort by signup month, show retained users by login month
WITH cohort AS (
SELECT user_id, DATE_TRUNC('month', signup_date) AS cohort_month
FROM users
),
activity AS (
SELECT l.user_id, DATE_TRUNC('month', l.login_date) AS activity_month
FROM logins l
)
SELECT
c.cohort_month,
a.activity_month,
COUNT(DISTINCT a.user_id) AS active_users
FROM cohort c
JOIN activity a ON c.user_id = a.user_id
GROUP BY c.cohort_month, a.activity_month
ORDER BY c.cohort_month, a.activity_month;
Question 3: Calculate monthly churn rate
Tables: subscriptions (user_id, start_date, end_date)
Requirement: Churned users per month, churn rate = churned / active users
WITH active_users AS (
SELECT DATE_TRUNC('month', start_date) AS month, COUNT(DISTINCT user_id) AS active_users
FROM subscriptions
GROUP BY DATE_TRUNC('month', start_date)
),
churned_users AS (
SELECT DATE_TRUNC('month', end_date) AS month, COUNT(DISTINCT user_id) AS churned_users
FROM subscriptions
WHERE end_date IS NOT NULL
GROUP BY DATE_TRUNC('month', end_date)
)
SELECT
a.month,
a.active_users,
COALESCE(c.churned_users, 0) AS churned_users,
ROUND(100.0 * COALESCE(c.churned_users, 0) / a.active_users, 2) AS churn_rate
FROM active_users a
LEFT JOIN churned_users c ON a.month = c.month
ORDER BY a.month;
Question 4: Calculate Daily Active Users
Table: user_activity (user_id, activity_date)
Requirement: DAU per day
SELECT activity_date, COUNT(DISTINCT user_id) AS dau
FROM user_activity
GROUP BY activity_date
ORDER BY activity_date;
Question 5: Revenue by marketing channel
Tables: orders (order_id, user_id, amount, order_date), users (user_id, channel)
Requirement: Total revenue per channel, monthly breakdown
SELECT
DATE_TRUNC('month', o.order_date) AS month,
u.channel,
SUM(o.amount) AS revenue
FROM orders o
JOIN users u ON o.user_id = u.user_id
GROUP BY DATE_TRUNC('month', o.order_date), u.channel
ORDER BY month, revenue DESC;
Question 6: Identify returning customers
Table: orders (order_id, customer_id, order_date)
Requirement: Customers with more than one order
SELECT customer_id
FROM orders
GROUP BY customer_id
HAVING COUNT(order_id) > 1;
Question 7: Top product by revenue per month
Table: sales (product_id, amount, sale_date)
WITH monthly_sales AS (
SELECT
DATE_TRUNC('month', sale_date) AS month,
product_id,
SUM(amount) AS revenue
FROM sales
GROUP BY DATE_TRUNC('month', sale_date), product_id
),
ranked AS (
SELECT *, RANK() OVER (PARTITION BY month ORDER BY revenue DESC) AS rnk
FROM monthly_sales
)
SELECT month, product_id, revenue
FROM ranked
WHERE rnk = 1;
Double Tap ♥️ For More