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

Using MS SQL WHERE

What is the Difference Between UNION and UNION ALL in SQL Server ...

What is the Use of DBCC Commands in SQL Server ?

What is The Concept of Piecemeal Restore on SQL Server ?

What are the System Data Collection Sets Predefined Inside SQL Se ...