๐ง SQL Interview Question (Tricky & Logic-Based)
๐
logins(user_id, login_date)
โ Ques :
๐ Find users who logged in for 3 or more consecutive days.
๐งฉ How Interviewers Expect You to Think
โข Understand consecutive date logic
โข Use date arithmetic smartly
โข Create groups using row-number difference trick
โข Avoid complex self-joins
โข Aggregate after forming streak groups
๐ก SQL Solution
WITH numbered_logins AS (
SELECT
user_id,
login_date,
ROW_NUMBER() OVER (
PARTITION BY user_id
ORDER BY login_date
) AS rn
FROM logins
),
grouped_logins AS (
SELECT
user_id,
login_date,
DATE_SUB(login_date, INTERVAL rn DAY) AS grp
FROM numbered_logins
)
SELECT
user_id
FROM grouped_logins
GROUP BY user_id, grp
HAVING COUNT(*) >= 3;
๐ฅ Why this question is powerful:
โข Tests advanced window function usage
โข Checks understanding of gaps & islands concept
โข Evaluates real-world product analytics thinking
โข Very common in growth / engagement analytics interviews
โค๏ธ React if you want more scenario-based SQL questions
Post #2079
2.01K
- โค 6