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 Deadlock in SQL Server ?

How to Learn MS SQL Server Version

What is the Difference Between DB Mirroring and Log Shipping in S ...

What is a Covered Index in SQL Server ?

Changing MS SQL Database Name with Query