SQL Server > Query

Return top n records in SQL Server

How to return top n records from the database in SQL Server?


To return top few records from the database, we use TOP keyword.

SELECT TOP(2) FirstName, LastName, Age FROM PersonalDetails

Above query will return Top 2 records (only FirstName, LastName, Age columns data) from PersonalDetails table.

SELECT TOP(2) * FROM PersonalDetails

Above query would return Top 2 records (all columns) from PersonalDetails table.

We can also pass number of records to return as parameter.

DECLARE @top int
SET @top = 5
SELECT TOP (@top) * FROM PersonalDetails

Above statements would return top 5 records from PersonalDetails table.

 Views: 1454 | Post Order: 44



Write for us