• A nice quest! I like the solution for its clever use of the index.

    However, you depend on a table to deliver the lookup-values for the age-column (in this case master..spt_values ) and you need pre-execution knowledge about all the possible ages in your table (in this case 0...100).

    Here's a suggestion:

    select * from

    (select distinct age as AgeGroup from #RunnersBig) S1

    cross apply

    (select top 2 * from #RunnersBig where age=agegroup order by time)S2

    I suspect it is not as fast as your solution, as it scans the index for the "distinct ages". But it would not require any knowledge about the values in the "ages" column.

    I wonder if there is a solution having both, a fast execution with index-seek on the one hand, and a general independence of the value distribution in the age-column.

    Kay