• dray (1/22/2009)


    In the video example, wouldn't the same 20 people be selected, just returned in a different order? If you had a table with all the names, you would need to first have a sub query select all the records and add a uniqueidentifier (newguid()), then from that set select the top 20 and order by the uniqueidentifier.

    When you have TOP and ORDER BY in the same statement, the ORDER BY is logically applied before the TOP.

    From the entry on TOP (http://msdn.microsoft.com/en-us/library/ms189463.aspx)

    If the query includes an ORDER BY clause, the first expression rows, or expression percent of rows, ordered by the ORDER BY clause are returned. If the query has no ORDER BY clause, the order of the rows is arbitrary.