Share via


How to: Specify the TOP Clause in Queries

The TOP clause returns only the first n or n percent rows from a query. The TOP clause is useful when you want to inspect a portion of your results to find out if your query does what you expect it to do, without taking the resources necessary to return all of the query results.

Note

This feature is only available if you are connecting to a Microsoft SQL Server database.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

To specify the TOP clause in queries

  1. Open a query from Solution Explorer or create a new one.

  2. From the View menu, click Properties Window.

  3. In the Properties Window, locate and expand the Top Specification property.

  4. Click the (Top) child property and set it to Yes.

  5. In the Expression child property, type an expression that has a numeric result (for example, "10" or "2*5").

  6. Click the Percent child property and indicate whether or not to treat the Expression property as a percentage of all rows returned (Yes) or as the absolute number of rows returned (No).

  7. If your query uses the ORDER BY clause, click the With Ties child property and choose Yes to display all rows in a group if only part of the group is included or No to truncate them.

As you work through the preceding procedure, notice that the TOP clause, displayed in the SQL pane, changes to reflect the current settings of the properties.

Note

You can also change the values of the child properties of the Top Specification by editing the TOP clause in the SQL pane.

See Also

Reference

Query Properties

Other Resources

Designing Queries and Views