Query Limits, and Distinct and Ordered Data

Learn about limiting the number of rows returned, returning unique values, and output ordering.

In addition to filtering, we can further customize our SELECT statements by using TOP, DISTINCT, and ORDER BY keywords.

Return fixed amount of rows

By default, a SELECT statement returns all rows from the table. If we add filtering options, the statement returns all rows that satisfy the criteria. We can change this behavior by limiting the number of rows returned:

SELECT TOP [MaxNumberOfReturnedRows] * FROM TableSchema.TableName;

With the TOP keyword, we can specify the maximum number of rows the SELECT statement should return. Let’s consider the following code, where we retrieve all rows where the Name column contains the letter “i”:

Get hands-on with 1400+ tech skills courses.