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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:03:56+00:00 2026-05-23T09:03:56+00:00

I have this sql table with people’s name and ages. Bob 28 Bryan 30

  • 0

I have this sql table with people’s name and ages.

Bob     28
Bryan   30
Jim     25
John    42
Bill    22
Sam     28
Tom     26

I would like to make a sql command to order all people by age desc, find a name in it, a return the preceding one, the founded and the next one with their position.

For example, admit that I would like to find Tom, my request should return :

Name    Age     Rank
Jim     25      2
Tom     26      3
Bob     28      4

Jim has the number 2 because Bill is the youngest

Is it possible to do something like this ?

Thanks in advance for any help

  • 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-23T09:03:56+00:00Added an answer on May 23, 2026 at 9:03 am

    SQL isn’t suited for row-based operations. There’s no easy way to do “find a row where some condition(s) = true, then return the previous row” in a single query. You can do it in a couple steps, though:

    a) Run one query to retrieve ‘Tom’ and his age (26).

    b) Run another query to get the next older person
    SELECT name, age FROM ... WHERE age > 26 ORDER BY age ASC LIMIT 1

    c) Repeat but for next younger:
    SELECT name, age FROM ... WHERE age < 26 ORDER BY age DESC LIMIT 1

    This’ll fetch people who are at least 1 year old/younger… You don’t specify what happens if there’s multiple people of the same age (e.g. There’s Fred who’s also 26, or Doug and Elmer who are both 25), so I’m ignoring those conditions.

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

Sidebar

Related Questions

I have this sql: ALTER TABLE dbo.ChannelPlayerSkins DROP CONSTRAINT FK_ChannelPlayerSkins_Channels but apparently, on some
I have this sql statement: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName]
I have an SQL table like this : sales(product,timestamp) I want to display a
If i have a sql table with column like this id version subversion 1
Hi there I have an SQL table that looks like this: CREATE TABLE IF
I have a SQL Server 2005 table like this: create table Taxonomy( CategoryId integer
I have SQL Server 2008 with a table called ProductCategories designed like this: Id
If i have a parameterized SQL statement like this: SELECT * FROM table WHERE
In MS Transact SQL, let's say I have a table (Orders) like this: Order
Say I have these tables: people(id, name), cars(person_id, car) and this query: SELECT c.car

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.