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

  • Home
  • SEARCH
  • 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 8960235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:37:22+00:00 2026-06-15T15:37:22+00:00

I am trying to create pagination of records from a sql statement, I was

  • 0

I am trying to create pagination of records from a sql statement, I was able to get next and previous for all but the last record shows the previous record as next due to the sql query only returning 1 row instead of 2. I am setting next based on the first result and previous as the second result.

The data:

  • Apple
  • Banana
  • Grape
  • Orange

The sql:

select * 
from (select top 1 fruit 
      FROM table 
      where fruit > 'Banana' 
      order by fruit asc) as x 

UNION ALL 

select * 
from (select top 1 fruit 
      FROM table 
      where fruit < 'Banana' order by fruit desc) as y

The result:

fruit
--------
Grape
Apple

This works great!

For the first record – I get the correct result for the first record being the true next.
For the last record – I get one result which is the second query that gets the previous record. However I have no way of knowing its the last record so it outputs as Next instead of Previous.

How would I determine that is the last record and the single row being returned is actually the previous?

OR

How can I have the query return a NULL row for the Next query? So I can test against a NULL result?

BONUS POINTS!

How can I have one query that will give me first record, last record and next and previous so I could enable circular pagination? If on first record show last record as previous. If on last record show next record as first record?

  • 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-06-15T15:37:23+00:00Added an answer on June 15, 2026 at 3:37 pm
    SELECT
      *
    FROM
    (
      SELECT
        MIN(fruit)   as first,
        MAX(fruit)   as previous
      FROM
        yourTable
      WHERE
        fruit < 'banana'
    )
    CROSS JOIN
    (
      SELECT
        MIN(fruit)   as next,
        MAX(fruit)   as last
      FROM
        yourTable
      WHERE
        fruit > 'banana'
    )
    

    Or, shorter, but I’m not sure that it would be more performant…

    SELECT
      MIN(CASE WHEN fruit < 'banana' THEN fruit END)   as first,
      MAX(CASE WHEN fruit < 'banana' THEN fruit END)   as previous,
      MIN(CASE WHEN fruit > 'banana' THEN fruit END)   as next,
      MAX(CASE WHEN fruit > 'banana' THEN fruit END)   as last
    FROM
      yourTable
    

    Or, as a final wierd option…

    SELECT
      (SELECT TOP 1 fruit FROM yourTable WHERE fruit < 'banana' ORDER by fruit ASC)  AS first,
      (SELECT TOP 1 fruit FROM yourTable WHERE fruit < 'banana' ORDER by fruit DESC) AS previous,
      (SELECT TOP 1 fruit FROM yourTable WHERE fruit > 'banana' ORDER by fruit ASC)  AS next,
      (SELECT TOP 1 fruit FROM yourTable WHERE fruit > 'banana' ORDER by fruit DESC) AS last
    

    (This may be usefull if you have thousands+ of records, as each is search quickly and separately.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create a new Dedicated Cache Role in Windows Azure but get the
Hello I am trying to create some simple pagination, but my php skills are
I'm trying to create a pagination class and use a variable from outside the
I am trying to create pagination on some pages that display rows from a
I'm trying to get some aggregate values from different tables, but my problem is
I'm trying to create pagination for search results using MySQL and ColdFusion. My intention
I am trying to create a dynamic pagination system for my website and I've
Trying to create a black line in my view to separate text blocks but
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I am trying to create ajax pagination on Blog Page.. What I need to

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.