I'm trying to build a mailbox where we can group the messages in x. If you put x to 20 you'll see messages 1-20 on the first page, opening the second page will show message 21-40 etc.
How do I efficiently query this? The best I could come up with is this:
select top 20 *
from tbl_messages
where
tnr_id not in
(
select top 40 tnr_id —20/40/60/80/…
from tbl_messages
order by dt_made desc, tnr_id desc
)
order by dt_made desc, tnr_id desc
Is there a more efficient way to do this? Databases used are SQL server, oracle & sybase.