>>> select TestScore from (select *, row_number() over (order by TestScore, StudentID) as Row from @StudentsScore) X >>>>>
set statistics time on set statistics profile on set statistics io on print 'Creating cte' ;with cte as (select *, row_number() over (order by TestScore, StudentID) as Row from @StudentsScore) select avg(TestScore*1.0) from (select TestScore from cte where Row = (@TotalStudents+1)/2 union all select TestScore from cte where Row = (@TotalStudents+2)/2) Y print 'Finished with the UNION select' print 'Creating cte' ;with cte as (select *, row_number() over (order by TestScore, StudentID) as Row from @StudentsScore) select avg(TestScore*1.0) from cte where Row between (@TotalStudents+1)/2 and (@TotalStudents+2)/2 print 'Finished with the BETWEEN select' set statistics io off set statistics profile off set statistics time off