Here's another fun query i found to train SQL for product interviews. I am really doubtful about it tho -
given the following table
tab_a: user_id, date (they entered platform)
find the top 100 users with the longest continuous streak of visiting the platform as of yesterday.
I defined "longest continuous streak" as the gap between two timestamps of the same user.
Here's my solution using a self join, anyone has any better idea?
select a.user_id,
age(a.date, b.date) as streak
from tab_a as a
inner join tab_a as b
using(user_id)
and a.date > date 'yesterday' -- PostgreSQL 'yesterday' syntactic sugar
order by streak desc
limit 100;