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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:27:47+00:00 2026-06-11T21:27:47+00:00

I want to get range ranking from a table using mysql query. the table

  • 0

I want to get range ranking from a table using mysql query.

the table is like this,

+------------+------+
| first_name | age  |
+------------+------+
| Kathy      |   2  |
| Jane       |   1  |
| Nick       |   3  |
| Bob        |   5  |
| Anne       |   4  |
| Jack       |   6  |
| Bill       |   8  |
| Steve      |   7  |
+------------+------+

and I want to get Jack’s ranking with 2 lower and upper rankers.

+------------+------+
| Anne       |   4  |
| Bob        |   5  |
| Jack       |   6  |
| Steve      |   7  |
| Bill       |   8  |
+------------+------+

Any idea how to write this query for a MySQL database?

  • 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-11T21:27:48+00:00Added an answer on June 11, 2026 at 9:27 pm

    Here is a very ugly way to do this by applying a rownumber to the records. By applying the rownumber to the records, then you you will be able to return rows if the ages are not consecutive (Demo with Non-consecutive ages):

    select age, first_name
    from 
    (
      select t1.age, t1.first_name, @rownum:=@rownum+1 AS rownum
      from yourtable t1, (SELECT @rownum:=0) r
      order by t1.age
    ) x
    where rownum  >= (select rownum
                      from 
                      (
                        select t.age, 
                          t.first_name, 
                          @rownum:=@rownum+1 AS rownum
                        from yourtable t, (SELECT @rownum:=0) r
                        order by t.age
                      ) x
                      where first_name = 'jack') - 2
     and rownum  <= (select rownum
                        from 
                        (
                          select t.age, 
                            t.first_name, 
                            @rownum:=@rownum+1 AS rownum
                          from yourtable t, (SELECT @rownum:=0) r
                          order by t.age
                        ) x
                        where first_name = 'jack') + 2;
    

    see SQL Fiddle with Demo

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

Sidebar

Related Questions

I want to get 100 and example from this string ?connect:100/username:example/ I searched in
I have a this table, where I store multiple ids and an age range
I want to get access to a IP range with this net feature: 68.241.45.0/20
i want to get some string, range from 0000 to 9999, that's to say,
I want to get a list of num like 10,100,1000,10000... I can do this
In a VSTO C# project I want to get a range of rows from
I want to get rows from a table based on different dates. Although values
I want get the number in this image, using python and PIL: my code:
I want to get visualized statistics from my data in mongodb using matplotlib, but
In addition to this question Find number range intersection I want to get the

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.