Monday, November 14, 2011

How to get nth record in sql

To get nth record in sql use the following sql query:
User with clause
with ordered as(select  *,ROW_NUMBER() over(order by community_ID) as id from dbo.Communities) select * from  ordered where id=nth



How to get first and last record in sql

To get first and last record in sql write the followig query.

select * from dbo.Communities where Community_ID in(select min(Community_ID) from dbo.Communities union select MAX(Community_ID)from dbo.Communities)