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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:16:54+00:00 2026-05-11T05:16:54+00:00

Say I have table with two columns: Name, Age. I want to design a

  • 0

Say I have table with two columns: Name, Age.

I want to design a query that will return the names and ages sorted by age, but to also have an additional column which will run from 1 to N (the last row).

So for a table with the following rows:

John, 28 Jim, 30 Mike, 28 

The following rows will be returned

John, 28, 1 Mike, 28, 2 Jim, 30, 3 

How can I do this? (I’m using MySQL, btw, if it makes a diff).

  • 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. 2026-05-11T05:16:55+00:00Added an answer on May 11, 2026 at 5:16 am

    In mysql you can:

    SELECT Row,Name,Age FROM (SELECT @row := @row + 1 AS Row, Name,Age FROM table1 )  As derived1 

    However , this next approach is generic (but you need to have unique values in one of the columns of the table , in this example I’ve used name but it could be any unique column)

       select rank=count(*), a1.name, a1.age    from table1 a1, table1 a2    where a1.name >= a2.name    group by a1.name , a1.age    order by rank 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a table with just two columns: name and mood .
Ok say I have a table with two columns. Entry_id and name. Entry_id is
Lets say we have a database table with two columns, entry_time and value. entry_time
I have two tables with similar columns - let's say table A with column
If I have three columns in my MySQL table people , say id, name,
Let's say I have a table with two columns, one column for the ID
I have two tables, lets say table1 and table2 with common columns, id and
Say I have a database called Poll and have these two table structures. poll
I have a view table which is a union of two separate tables (say
I have two models, say Product and Bundle. products table is related to product_prices

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.