Hi
I have student details table. I want to get the second last record details of student
Thanks
Hi
I have student details table. I want to get the second last record details of student
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit MohantyPosted Jun 19, 2023, 7:42 AM
Vishal YelvePosted Jun 23, 2023, 7:50 AM
Kisorjan JakathiswaranPosted Jun 23, 2023, 6:20 AM
Here is the query to get the second last record in MySQL
Deepak RawatPosted Jun 20, 2023, 5:11 AM
To get the second last record details of a student from the student details table, you can use SQL queries with appropriate filtering and ordering. Here's an example assuming you have a "Students" table with columns "StudentID" and "StudentName":
Brahma Prakash ShuklaPosted Jun 19, 2023, 7:37 AM
SELECT TOP 1 StudentID, StudentName
FROM Students
ORDER BY StudentID DESC
OFFSET 1 ROW
FETCH NEXT 1 ROW ONLY;
Jignesh KumarPosted Jun 18, 2023, 4:36 AM
Hello Ramco,
wa can do below one in verious way but if you are using sql server version 2012 or above then best way to do this one as below,
Here :
OFFSET : The number of rows to skip before starting to return rows from the query
FETCH NEXT : the number of rows to return after the
OFFSETclause has been processed.Prasad RaveendranPosted Jun 18, 2023, 1:59 AM
To retrieve the second last record from a student details table using SQL, you can make use of the
ORDER BYandLIMITclauses. Here's an example query assuming you have a table namedstudent_detailswith a primary key column namedid:Explanation:
ORDER BYclause is used to sort the records in descending order based on theidcolumn. Assuming higheridvalues are more recent, this will arrange the records from the most recent to the oldest.LIMITclause is used to limit the number of records returned. In this case, we set it to1to retrieve only one record.OFFSETclause is used to skip a certain number of records. By setting it to1, we skip the first record (the most recent) and retrieve the second last record.Make sure to replace
student_detailsandidwith the actual table and column names in your database.Mukesh SagarPosted Jun 17, 2023, 3:22 PM
SELECT top 1 * FROM Student WHERE id < (SELECT MAX(id) FROM Student)
order by Id desc