Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Churn Prediction with SQL | Analytical SQL Techniques
SQL for Analytical Reports
Abschnitt 1. Kapitel 15
single

single

bookChurn Prediction with SQL

Swipe um das Menü anzuzeigen

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
Aufgabe

Wischen, um mit dem Codieren zu beginnen

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 15
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt