SpinTheMic
Pro

Explain it in 60 seconds

Gaps and islands problems

Data Engineering · SQL · Experienced

What is a gaps-and-islands problem, and how do you solve it in SQL?

What a strong answer covers

Your AI report card counts how many of these ideas you cover.

  • Islands are consecutive runs of values, gaps are breaks between them.
  • Classic trick: subtract ROW_NUMBER from the sequence value to get a group key.
  • For dates: date minus ROW_NUMBER days is constant within a consecutive streak.
  • Then GROUP BY that key to get start, end and length.
  • LAG-based flags plus running SUM is another approach.
Practise this topic

Start without signing up. Sign in free to get an AI report card on your answer.

See all topics