Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 587243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:11:32+00:00 2026-05-13T15:11:32+00:00

I’m trying to build a mailbox where we can group the messages in x.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:11:32+00:00Added an answer on May 13, 2026 at 3:11 pm

    In Oracle:

    SELECT  *
    FROM    (
            SELECT  t.*, rownum AS rn
            FROM    tbl_messages t
            ORDER BY
                    dt_made DESC, tnr_id DESC
            )
    WHERE   rn > 40
            AND rownum <= 20
    

    In SQL Server 2005 and above:

    DECLARE @start INT
    DECLARE @pagesize INT
    SET @start = 40
    SET @pagesize = 20  
    
    SELECT  *
    FROM    (
            SELECT  TOP (@start + @pagesize)
                    t.*, ROW_NUMBER() OVER (ORDER BY dt_made DESC, tnr_id DESC) AS rn
            FROM    tbl_messages t
            ORDER BY
                    dt_made DESC, tnr_id DESC
            ) q
    WHERE   rn > @start
    

    ROW_NUMBER is supported by Oracle too, but due to implementation details is a little bit less efficient than rownum.

    See this article in my blog for performance comparison:

    • Oracle: ROW_NUMBER vs ROWNUM

    Update:

    If you can tolerate some discrepancies resulted from the concurrent updates, you can remember the last record on the current page of the client side and use it to get the next results faster:

    SELECT  TOP 20 *
    FROM    tbl_messages t
    WHERE   dt_made <= @last_dt_made
            AND NOT (dt_made = @last_dt_made AND tnr_id >= @last_tnr_id)
    ORDER BY
            dt_made DESC, tnr_id DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.