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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:27:28+00:00 2026-05-23T00:27:28+00:00

My sql database stores the highscores for the players of a game like this:

  • 0

My sql database stores the highscores for the players of a game like this:

playerName
scores

To show the scores I’m trying to create a query that returns for a playerId “joe” the positon of joe as well as one player with more and less points than joe:

(rank) (playerName) (scores)
5000    luci       2001
5001    joe        1900
5002    marc       1750

(I added the rank here just to make it more clear. It isn’t stored and represents just the position in the list when sorting by the scores)
Is there a way to do soemthing like this with a query without getting half of the database as result?

  • 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-23T00:27:28+00:00Added an answer on May 23, 2026 at 12:27 am

    You’d ideally want to be using window functions to do this, namely row_number(), lead() and lag().

    These are not available in MySQL, but here are a few workarounds (also row_number(), picked up from the same author).


    Using window functions:

    with
    players as (
    select row_number() over w as rank,
           playername,
           scores
    from players
    window w (order by scores desc)
    ),
    player as (
    select rank
    from players
    where playername = 'joe'
    )
    select rank,
           playername,
           scores
    from players
    join player on players.rank between player.rank - 1 and player.rank + 1
    

    Alternatively (slightly faster):

    with
    player_range as (
    select scores - 100 as scores -- or whatever is large enough based on your stats
    from players
    where playername = 'joe'
    ),
    players as (
    select row_number() over w as rank,
           players.playername,
           players.scores
    from players
    join player_range
    on player_range.scores <= players.scores
    window w (order by scores desc)
    ),
    player as (
    select rank
    from players
    where playername = 'joe'
    )
    select rank,
           playername,
           scores
    from players
    join player on players.rank between player.rank - 1 and player.rank + 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have questions which are represented like this in my SQL database : CREATE
Ok I have a table in my SQL Server database that stores comments. My
I am making an SQL database that stores contacts. I want to be able
I have a table in my SQL database that stores an AccountID and an
I have a sql database that stores some documents. A user can sign into
I have a SQL Server 2005 database that stores data for multiple users. Each
I have a site that stores a bunch of records in an sql database
I have a varchar field in a SQL Server database that stores phone numbers
I have a application that stores passwords in a ms sql database, I wish
I have a MS SQL database that I am working on, within this database

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.