SQL SELECT TOP Clause

SELECT TOP CLUASE

SQL SELECT TOP clause is used to retrieve top N records or by percentage value from specified table(s) and limits the records by mentioned value in the query. If you want to fetch records in a specific order then you can mention ORDER BY clause in conjunction with SELECT TOP clause otherwise you will get random TOP N records from table.

Syntax

SELECT TOP <Number | Percent> ColumnName(s)

FROM TableName

We will take few examples to understand SELECT TOP clause. We will use Student table and fetch top 10 records from the table as given below.

SELECT TOP 10 *FROM Student

TOPClause1

Now we will select specific columns FirstName, LastName and Country from Student table.

SELECT TOP 10 FirstName, LastName, Country FROM Student

TOPClause2

Here, we will see example for TOP N PERCENT of Student table as shown below.

SELECT TOP 8 PERCENT * FROM Student

TOPPercent1

Now we will use ORDER BY clause with TOP N records in Student table so that we can get sorted records.

SELECT TOP 10 FirstName, LastName, Country FROM Student
ORDER BY LastName DESC

TOPOrderBy

Reference: Manzoor Siddiqui [www.SQLServerLog.com]

CLICK HERE to watch live practical.

You may also like

You may also like...

Leave a Reply