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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:03:30+00:00 2026-06-07T13:03:30+00:00

So I have this MySQL 5.0.24 table select state, county, population from state_pops order

  • 0

So I have this MySQL 5.0.24 table

select state, county, population from state_pops order by state, county;

yields

Arizona   Yapavai    4
Arizona   Pueblo     5
Arizona   Pinal      8
Arizona   Maricopa  13
Michigan  Lawson     3
Michigan  Maple      4
Michigan  Appleton   8
Texas     Richmond   5
Texas     Dupree     7
Texas     Brighton  10

I need to determine which is the most heavily-populated county in each state.

select state, county, max( population) from state_pops group by state order by state;

yields

Arizona   Maricopa  13
Michigan  Appleton   8
Texas     Brighton  10

Easy. But now I need to somehow flag the most-populous county of each state while
listing all counties of all states, like so

Arizona   Yapavai    4  NO
Arizona   Pueblo     5  NO
Arizona   Pinal      8  NO
Arizona   Maricopa  13  YES
Michigan  Lawson     3  NO
Michigan  Maple      4  NO
Michigan  Appleton   8  YES
Texas     Richmond   5  NO
Texas     Dupree     7  NO
Texas     Brighton  10  YES

So I need to derive a column somehow, perhaps some form of CASE..WHEN Any ideas?

TIA,

Still-learning Stev

  • 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-07T13:03:32+00:00Added an answer on June 7, 2026 at 1:03 pm

    You can use this solution:

    SELECT a.state, 
           a.county, 
           a.population, 
           COALESCE(b.isMostPop, 'NO') AS flag
    FROM state_pops a
    LEFT JOIN
    (
        SELECT state, MAX(population) AS maxpop, 'YES' AS isMostPop
        FROM state_pops
        GROUP BY state
    ) b ON a.state = b.state AND a.population = b.maxpop
    

    See a SQL-Fiddle Demo

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

Sidebar

Related Questions

I have an table structure like this mysql> SELECT id, name, parent_id FROM categories;
I have a table which looks like this: mysql> SHOW COLUMNS FROM Users; +------------+--------------+------+-----+---------+----------------+
I have this mysql table called comments which looks like this: commentID parentID type
I have this in a MySQL db: table Message sender_id int recipient_id int created
I have a mysql table that looks like this: id col2 col3 col4 1
I have a MySQL table like this: Payments +----+---------------------+---------+ | id | date (sorted
I have a MySQL table structured somewhat like this: type name value ===================== 1
I have a MySQL table looking like this: > describe books; +---------------+--------------+------+-----+---------+----------------+ | Field
I have a MySQL table that looks like this: `id` int(10) unsigned NOT NULL
I have a MySQL table that looks something like this: +-----+------------+ | id |

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.