Code Analysis

BP006

TOP without ORDER BY

TOP is being used in a SELECT statement without a subsequent ORDER BY clause.

This is legal in SQL but meaningless because asking for the TOP 10 rows implies a certain order, and tables have no implicit logical order.


Available in

SQL Prompt

SQL Code Guard

Using TOP in a SELECT statement without a subsequent ORDER BY clause is legal in SQL Server, but meaningless because asking for the TOP 10 rows implies that the data is guaranteed to be in a certain order, and tables have no implicit logical order. You must specify the order.

In a SELECT statement, you should always use an ORDER BY clause with the TOP clause, to specify which rows are affected by the TOP filter. If you need to implement a paging solution in an application widget, to send chunks or “pages” of data to the client so a user can scroll through data, it is better and easier to use the OFFSET–FETCH sub-clause in the ORDER BY clause, instead of the TOP clause.

See TOP without ORDER BY in a SELECT statement 
for examples.


Didn't find what you were looking for?