SQL Practice Logo

SQLPractice Online

Scenario SQL Question

Consecutive-Year Salary Record Streaks (Gaps & Islands)

Build mastery in Ctes Window with this scenario-based SQL challenge and live execution support.

Problem Statement

In salaries_history, each employee has records in consecutive years. Use the classic gaps-and-islands trick (year − ROW_NUMBER() is constant within a run) to collapse each run into one streak. Return employee_id, streak_start_year, streak_end_year, years_in_streak — ordered by employee_id, streak_start_year.

Relevant Tables

salaries_history

SQL Concepts Used in This Scenario

Strengthen your understanding with these targeted learning topics: