Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Churn Prediction with SQL | Analytical SQL Techniques
SQL for Analytical Reports
Sección 1. Capítulo 15
single

single

bookChurn Prediction with SQL

Desliza para mostrar el menú

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
Tarea

Desliza para comenzar a programar

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.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 15
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt