Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Get top n records in SQL Server 2008 using PL/SQL

Author: Zunnair

In this simple article you will learn that how to retrieve top n records from database using PL/SQL

 Some time we need some specific number of records from database table then we use TOP keyword in PL/SQL.

 

If we want to get 2 records from the database then we use Top keyword in our query.

 

Syntax:

 

SELECT TOP 2 [CatID],[Name],[Description],[CreationDate],[Active]

  FROM [Categories]

 

Result:

 

CatID Name              Description       CreationDate      Active

1     Entertainment     Entertainment     2009-12-12        1

2     Social            Social            2009-10-10       1

 

 

Now this command retrieve 2 records only from the database.

 

This is simple query for this purpose but if we want to get last two records then what we need to do. For that we can use order by clause with TOP key word in decending order.

 

Syntax:

 

SELECT TOP 2 [CatID],[Name],[Description],[CreationDate],[Active]

  FROM [Categories]

  order by catid desc

 

Result:

 

CatID Name              Description       CreationDate      Active

44    People            People            2009-12-12        1

43    Life              Life              2009-10-10       1

 

 

These are some different ways to do these operation and TOP keyword helps us in many senerios

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with SET ROWCOUNT in SQL Server 2008 using PL/SQL

Disclaimer - Privacy
© 2002-2017 DevASP.net