Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Churn Prediction with SQL | Analytical SQL Techniques
SQL for Analytical Reports
セクション 1.  15
single

single

bookChurn Prediction with SQL

メニューを表示するにはスワイプしてください

Churn prediction is a crucial analytical process for identifying customers or users who are likely to stop using a product or service. In the context of SaaS or subscription businesses, churn prediction typically relies on analyzing user activity and transaction patterns to flag those at high risk of leaving. By understanding and detecting these behavioral signals early, companies can proactively engage with at-risk users, aiming to retain more customers and reduce revenue loss.

To predict churn, you often analyze user activity logs, transaction histories, and subscription records. A common SQL approach is to flag users who have not performed any activity—such as logins, purchases, or payments—within a recent time window (for example, the last 90 days). This is especially relevant for SaaS platforms, where regular engagement is expected. You can adapt this logic to your available data—such as using order dates, payment dates, or subscription changes as proxies for engagement.

Note
Definition

Churn refers to the loss of customers or users who stop using a product or service. In SQL analytics, common churn indicators include extended periods of inactivity (e.g., no logins or transactions for 30, 60, or 90 days), subscription downgrades, cancellations, or payment failures.

12345678910111213
SELECT u.user_id, u.username, u.full_name, MAX(p.payment_date) AS last_payment FROM users u LEFT JOIN subscriptions s ON u.user_id = s.user_id LEFT JOIN payments p ON s.subscription_id = p.subscription_id GROUP BY u.user_id, u.username, u.full_name HAVING MAX(p.payment_date) IS NULL OR MAX(p.payment_date) < '2025-01-01'::date - INTERVAL '90 days' ORDER BY last_payment ASC NULLS FIRST;
copy

Suppose the SaaS customer success team needs a current list of users who are likely to churn, based on recent inactivity. They want to prioritize outreach to these users to prevent churn and improve retention.

12345678910111213141516
SELECT u.user_id, u.username, u.full_name, MAX(p.payment_date) AS last_payment FROM users u LEFT JOIN subscriptions s ON u.user_id = s.user_id LEFT JOIN payments p ON s.subscription_id = p.subscription_id AND p.status = 'Completed' GROUP BY u.user_id, u.username, u.full_name HAVING MAX(p.payment_date) IS NULL OR MAX(p.payment_date) < CURRENT_DATE - INTERVAL '90 days';
copy
タスク

スワイプしてコーディングを開始

Write a SQL query to list all users who have expired or changed their subscription plan.

  • Use the users and subscriptions tables.
  • Select user_id, username, full_name, plan, status, and end_date.
  • Include users whose subscription status is 'Expired', or whose current plan differs from their plan_at_signup.
  • Only include subscriptions where end_date >= '2023-01-01'.
  • Order results by end_date descending.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  15
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt