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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Jun 19, 2023, 3:42 AM EDT
SELECT TOP 1 * FROM ( SELECT TOP 2 * FROM StudentDetailsTable ORDER BY StudentID DESC ) AS SubQuery ORDER BY StudentID ASC; //OR SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY StudentID DESC) AS RowNum FROM StudentDetails ) AS SubQuery WHERE RowNum = 2;Mukesh SagarRank #640 · 3.8K reputationPosted Jun 17, 2023, 11:22 AM EDT
SELECT top 1 * FROM Student WHERE id < (SELECT MAX(id) FROM Student)
order by Id desc
Prasad RaveendranRank #320 · 7.8K reputationPosted Jun 17, 2023, 9:59 PM EDT
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:SELECT * FROM student_details ORDER BY id DESC LIMIT 1 OFFSET 1;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.Jignesh KumarRank #91 · 22.9K reputationPosted Jun 18, 2023, 12:36 AM EDT
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,
SELECT CustomerID FROM custiners ORDER BY CustomerID DESC OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY;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.Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Jun 19, 2023, 3:37 AM EDT
SELECT TOP 1 StudentID, StudentName
FROM Students
ORDER BY StudentID DESC
OFFSET 1 ROW
FETCH NEXT 1 ROW ONLY;
Deepak RawatRank #646 · 3.7K reputationPosted Jun 20, 2023, 1:11 AM EDT
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":
SELECT TOP 1 * FROM Students WHERE StudentID < (SELECT MAX(StudentID) FROM Students) ORDER BY StudentID DESC;Kisorjan JakathiswaranRank #1,688 · 903 reputationPosted Jun 23, 2023, 2:20 AM EDT
Here is the query to get the second last record in MySQL
SELECT *FROM (SELECT *FROM yourTableName ORDER BY yourIdColumnName DESC LIMIT 2) anyAliasName ORDER BY yourIdColumnName LIMIT 1;SELECT * From (select * from student ORDER BY ID DESC LIMIT 2) AS studentlastsecondrecord ORDER BY ID LIMIT 1;Vishal YelveRank #178 · 13.6K reputationPosted Jun 23, 2023, 3:50 AM EDT
SELECT TOP 1 * From (select Top 2 * from Students ORDER BY StudentId DESC) x ORDER BY StudentId; OR select * from Students where StudentId=(select max(StudentId)-1 from Students); OR SELECT StudentId, StudentName FROM Students ORDER BY StudentId desc LIMIT 1, 1;