TOP Keyword in MS SQL Server


In SQL Server TOP keyword is use get top n number of record in a controlled result.

Let consider below list of student with there class, marks, city and contact details.

select top record from sql table example

Select TOP x record from sql table


Select top 5 student data.

Example

SELECT TOP 5 * FROM tblStudent

Output

select top record from sql table with ascending format


Note : While using TOP we have to specify how many not of top record we need. In above example we specify that we need top 5 student record.


TOP with ORDER BY


We can use the TOP keyword with ORDER BY clause. If We want to find out top 2 students who scores higher marks, and they belong from Pune city.
Let see below example.

Example

SELECT TOP 2 * FROM tblStudent WHERE City= 'Pune'
WHERE Marks DESC

Output

select top record from sql table with descending format