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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:37:54+00:00 2026-05-14T21:37:54+00:00

Given table USER (name, city, age), what’s the best way to get the user

  • 0

Given table USER (name, city, age), what’s the best way to get the user details of oldest user per city?

I have seen the following example SQL used in Oracle which I think it works

select name, city, age 
from USER, (select city as maxCity, max(age) as maxAge 
            from USER 
            group by city)
where city=maxCity and age=maxAge

So in essence: use a nested query to select the grouping key and aggregate for it, then use it as another table in the main query and join with the grouping key and the aggregate value for each key.

Is this the standard SQL way of doing it? Is it any quicker than using a temporary table, or is in fact using a temporary table interanlly anyway?

  • 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-14T21:37:55+00:00Added an answer on May 14, 2026 at 9:37 pm

    What you are using will work, although it displays all users which share the max age.

    You can do this in a slightly more readable way using the row_number() ranking function:

    select name, city, age 
    from (
        select 
            city
         ,  age
         ,  row_number() over (partition by city order by age) as rn
         from USER
    ) sub
    where rn = 1
    

    This will also select at most one user per city.

    Most database systems will use a temporary table to store the inner query. So I don’t think a temporary table would speed it up. But database performance is notoriously hard to predict from a distance 🙂

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

Sidebar

Related Questions

Given a table of logical resource identifiers (one per row), what is the best
I have a table of 100,000s of USERS (name, age, gender, phone, company, street,
Given a table in MySQL with rows that have a 'Country' field, I need
Given a table row, I want to get the HTML out of the span
I have a MySQL database and I am storing user records in the table
Given the following tables: Resources: ID int, Name varchar(100), Address varchar(500), City varchar(100), etc.
Consider a simplified table schema of a StackOverflow-like system: Tables: User ( id, name
I have two tables User id name Result id user_id score created_at Every hour
I have this problem. Given a users table that consists of users' username in
I want to list down all the column details for a given table and

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.