.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to get the second last record from a Table.

Posted By:Syed Shakeer Hussain       Posted Date: March 23, 2010    Points: 10    Category: SQL Query    URL: http://www.dotnetspark.com  

Below is the Query to get the second last record from a Table in SQL Server.
 

insert into dupemp(name,salary,deptno) values('1sd',4000,12)

insert into dupemp(name,salary,deptno) values('2sd',4000,12)

insert into dupemp(name,salary,deptno) values('3sd',4000,12)

insert into dupemp(name,salary,deptno) values('6sd',4000,12)

insert into dupemp(name,salary,deptno) values('4sd',4000,12)

Query:-

with CTE as(select *,ROW_NUMBER() over(order by (select 0))as row from dupemp )
select * from cte where row=(select min(row) from cte where row in(select top 2 row from cte order by row desc))


Output:-

Name salary deptno

6sd  4000    12


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend