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 8038343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:12:21+00:00 2026-06-05T03:12:21+00:00

I realize that I can do this in code, but I was curious if

  • 0

I realize that I can do this in code, but I was curious if there’s a straight SQL way to select (for instance) 10 results in addition to the one that your query matches…

so, let’s say I had a database of players all of whom had scores and I wanted to show a user the 5 players whose scores are behind theirs and the 5 ahead… so, I select their row but now should I just use an or in the where clause and say something like…

select score as selectedScore from player_scores where playerId = xxxx
or (score > selectedScore - 5 and score < selectedScore + 5)

I realize that this implies all the scores are in exact order, and I’d modify the query to pull the userIds from those scores, so this is just a pseudo example for the sake of the question.

I suspect that would work, but is that the *right way to approach this?

  • 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-05T03:12:22+00:00Added an answer on June 5, 2026 at 3:12 am

    You can use UNION to combine two queries that use ORDER BY and LIMIT to select the next 5 and the previous 5 respectively; by using a self-join in each query, one can find all other players adjacent to the desired player’s score (this assumes that playerId is unique in player_scores however):

    (
      SELECT   p1.playerId, p1.score
      FROM     player_scores AS p1 JOIN player_scores AS p2 ON p1.score >= p2.score
      WHERE    p2.playerId = ?
      ORDER BY p1.score ASC
      LIMIT    6
    ) UNION ALL (
      SELECT   p1.playerId, p1.score
      FROM     player_scores AS p1 JOIN player_scores AS p2 ON p1.score <  p2.score
      WHERE    p2.playerId = ?
      ORDER BY p1.score ASC
      LIMIT    5
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize that this can be a matter of preference, but I have noticed
I recently use this code, and realize that in anonymous class, I can't access
Of course I realize that there's no one right way to design a SQL
I realize that without code this might be hard to answer but the problem
I realize that this question has been asked 100times but none that I have
I realize that a whole lot of code cannot fit here, but I am
I was trying so many options, but no results. However, I have this code
I realize that it can depend on certain things (and obviously how efficient the
I realize that I need to use a mailserver so people can contact me,
I realize that the answer to this question is likely quite obvious (if somewhat

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.