Using MS SQL TOP

The TOP function in MS Sql is used to retrieve the top desired number of rows from a written SELECT query. That's why TOP is often used in conjunction with the ORDER BY function to sort rows.

Let's examine the example below..

Let's have 2 columns named NAME and PRICE in our table named PRODUCTS. In this table where there are 7 records, we want to bring the first 3 products in order of names.

NAME PRICE
Fruit Juice 5
Cake 10
Beer 12
Soda 8
Water 6
Salt 15
Sugar 16

For this, let's write a query using the TOP function.

SELECT TOP(3) * FROM PRODUCTS ORDER BY NAME

Below is the result of our query.

NAME PRICE
Beer 12
Cake 10
Fruit Juice 5

We sorted by ORDER BY according to the NAME column and brought the first 3 records from this order.



You May Interest

What is the Use of Dedicated Admin Connection ?

What is the Filtered Index in SQL Server ?

What is the Maximum Size per Database for SQL Server Express ?

How to Optimize Stored Procedure in SQL Server ?

Adding Columns to a Table with a Query in MS SQL