I have a table (Article), it has 10 records, I want to get the last four records (6th, 7th, 8th, 9th) without 10th. The Query may also work for more than 10 records to get the last four rows without the absolute last one.
Thanks in Advance!
I have a table (Article), it has 10 records, I want to get the
Share
In SQL, tables are inherently unordered. So, let me assume that you have a column that specifies the ordering — an id column, a date time, or something like that.
The following does what you want:
If for some reason you don’t have an id, you can take your chances with the following query:
I want to emphasize that this is not guaranteed to work. In my experience, I have seen that definition of seqnum assign sequential number to rows in order. BUT THIS IS NOT GUARANTEED TO WORK, and will probably not work in a multi-threaded environment. But, you might get lucky (particularly if your rows fit on one data page).
By the way, you can use the same idea with a real column: