← All SQL interview questions

Longest daily login streak per user

HardWindow FunctionsSwiggyMeesho

The product team measures habit formation by the longest run of consecutive calendar days on which a user logged in. Users can log in several times a day, and days can be skipped.

From user_logins, return for each user user_id, longest_streak_days, streak_start and streak_end for their longest streak. If a user has several streaks of the same length, return the earliest one. Order by user_id.

Sample data

Table: user_logins

21 row(s) · showing first 8

login_idINTuser_idINTlogin_dateDATE
112026-10-01
212026-10-02
312026-10-03
412026-10-03
512026-10-05
612026-10-06
712026-10-07
812026-10-08