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

single

bookChurn Prediction with SQL

Veeg om het menu te tonen

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
Taak

Veeg om te beginnen met coderen

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 15
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt