๐ง SQL Interview Question (ModerateโTricky & Retention Analysis)
๐
subscriptions(user_id, start_date, end_date)
โ Ques :
๐ Find users who renewed their subscription immediately after the previous one ended (no gap between subscriptions).
๐งฉ How Interviewers Expect You to Think
โข Sort subscriptions by start_date for each user
โข Use a window function to access the previous subscription end date
โข Check if the next start_date equals the previous end_date
๐ก SQL Solution
WITH sub_cte AS (
SELECT
user_id,
start_date,
end_date,
LAG(end_date) OVER (
PARTITION BY user_id
ORDER BY start_date
) AS prev_end_date
FROM subscriptions
)
SELECT DISTINCT user_id
FROM sub_cte
WHERE start_date = prev_end_date;
๐ฅ Why This Question Is Powerful
โข Tests ability to analyze subscription lifecycle data
โข Evaluates knowledge of window functions for sequential comparisons
โข Similar logic used in retention and churn analysis
โค๏ธ React if you want more real interview-level SQL questions like this. ๐
Post #2108
1.79K
- โค 4