Malay Dave Malay Dave - 15 days ago 5
SQL Question

How to find second last record from the table using SQL query?

I have student table and there is 10 record in this table. But i want to find out the second last record and i have only table name so how can i found that record?

here is my query .


select * from student order by primarykey desc LIMIT 1,1


Thanks in advance.

Answer

Use Bellow query:

 select top 1 * from [Your_Table_Name] where [Your_Table_Primary_Name] in
     (select top 3 [Your_Table_Primary_Name] from [Your_Table_Name] order by
      [Your_Table_Primary_Name] desc) 
order by [Your_Table_Primary_Name] asc

Description


1. first you need to select last three records using bellow syntax

select top 3 [Your_Table_Primary_Name] from [Your_Table_Name] order by [Your_Table_Primary_Name] desc

2. after then you need to select first record using bellow syntax

select top 1 * from [Your_Table_Name] where [Your_Table_Primary_Name] in (***First Syntax/Above described Syntax*****) order by [Your_Table_Primary_Name] asc

Comments