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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:01:25+00:00 2026-06-01T12:01:25+00:00

I have a table, table 1. In this table is name, city, price. The

  • 0

I have a table, table 1. In this table is name, city, price. The names are all unique, the cities can be reused. I want to make a query that gives me the highest price of each city, and the name associated with that row. To get the highest price of each city I just do:

SELECT MAX(price), city FROM table1 GROUP BY city

But I want the name as well. If I try

SELECT MAX(price), city, name FROM table1 GROUP BY city

Then I just get a query error about how name isn’t included in the group by. If I add name to the group by, then it doesn’t do what I want. I can think of how I could do this using two queries, but I bet there’s a way to do it in just one. What am I missing?

I’ve found a few similar questions on stackoverflow, but they all seem to be doing something more complicated than I’m attempting to. I’ve only ever used simple SQL queries, so I’m educating myself on some of the more advanced features.

  • 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-01T12:01:26+00:00Added an answer on June 1, 2026 at 12:01 pm
    WITH RankedPrices AS (
        SELECT *, ROW_NUMBER() OVER (PARTITION BY city ORDER BY price DESC) AS ix
        FROM table1
    )
    SELECT * FROM RankedPrices WHERE ix=1;
    

    Note that you didn’t specify what should happen if two products in a city are both the most expensive and equal in their price. This query here returns any of the two (but only one), you could also handle this differently.

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

Sidebar

Related Questions

I have a table looking something like this ID Name City ------------- 0 asd
hi there imagine I have a table like this Id | Name | City
I have a table like this: name code group john 12 smith 15 how
Let's say I have a table like this: name | score_a | score_b -----+---------+--------
Let's say I have a table like this: name |order_id ======================= first_record | 0
I have this table : Name | money_earned _______________________ john | 100 paul |
I have a table in mysql like this Name Result T1_09_03_2010 fail T2_09_03_2010 pass
I have a table that looks like this: Name Job Year ImpFile ImpDate ------------------------------------------------
I have a table that looks something like this: Name Year Value A 2000
I have a table called 'Audit' in SQL Server 2005 like this: Name |

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.