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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:13:48+00:00 2026-06-09T14:13:48+00:00

I have been intrigued by a problem on SQLZoo. It is a greatest-n-per-group problem.

  • 0

I have been intrigued by a problem on SQLZoo. It is a “greatest-n-per-group” problem. I would like to understand how the engine is operating.

A table called bbc contains the name, region of the world and population of each country:

bbc( name, region, population)

The given task is to select the most populous country of each region, showing its name, the region and population.

The solution provided is:

SELECT region, name, population FROM bbc x
  WHERE population >= ALL
    (SELECT population FROM bbc y
        WHERE y.region=x.region
          AND population>0)

1. Main Question. I am finding this a bit of a mind twister. I would like to understand how the engine processes this, because at first blush it seems there is some kind of co-dependence (x depending on y, and y depending on x). Does the engine follow some kind of recursion to produce the final selection? Or am I missing something, such that either x or y is actually fixed?

2. Secondary Question. Oddly, when I pull the “AND population>0” out of the parenthesis and leave it on its own at the bottom, one of the regions (Europe / Russia) goes missing from the 8 results. Why? I don’t understand that.

And indeed, when I try the query on the world database (available from the mySQL website on the same page as Sakila), the behavior is different:
With population > 0 out of the parentheses, I get 6 regions. Six is the right number in this database, because “SELECT continent FROM country GROUP BY continent” reveals seven continents, of which one is Antarctica, which includes 5 “countries”, all with a 0 population.

So that seems right.

SELECT continent, `name`, population FROM country X
WHERE population >= ALL 
(SELECT population FROM country Y
WHERE Y.`Continent` = X.`Continent`)
AND population>0

On the other hand, when I pull “population > 0” back into the parentheses as on SQLZoo, I also get 5 countries with a zero (the countries “belonging to Antarctica”). It doesn’t matter if I specify x.population or y.population, I get zeroes.

continent      name                                          population  
-------------  --------------------------------------------  ------------
Antarctica     Antarctica                                               0
Antarctica     French Southern territories                              0
Oceania        Australia                                         18886000
South America  Brazil                                           170115000
Antarctica     Bouvet Island                                            0
Asia           China                                           1277558000
Antarctica     Heard Island and McDonald Islands                        0
Africa         Nigeria                                          111506000
Europe         Russian Federation                               146934000
Antarctica     South Georgia and the South Sandwich Islands             0
North America  United States                                    278357000

Very much looking for insights on these questions!

Wishing you all a beautiful week.

🙂

Notes:

  1. For reference, the problem is number 3a on this page:
    http://old.sqlzoo.net/1a.htm?answer=1

  2. A thread mentioning the “greatest-n-per-group” problem for the same query:
    MySQL world database Trying to avoid subquery

  3. The world database is available here: http://dev.mysql.com/doc/index-other.html

  • 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-09T14:13:49+00:00Added an answer on June 9, 2026 at 2:13 pm
    1. Main Question. I am finding this a bit of a mind twister. I would like to understand how the engine processes this, because at first
      blush it seems there is some kind of co-dependence (x depending on y,
      and y depending on x). Does the engine follow some kind of recursion
      to produce the final selection? Or am I missing something, such that
      either x or y is actually fixed?

    This isn’t recursion. See this from the MySQL docs. Their solution to the problem is equivalent to this

    SELECT region, name, population FROM bbc x
      WHERE population = 
        (SELECT max(population) FROM bbc y
            WHERE y.region=x.region
              )
    
    1. Secondary Question. Oddly, when I pull the “AND population>0” out of the parenthesis and leave it on its own at the bottom, one of the
      regions (Europe / Russia) goes missing from the 8 results. Why? I
      don’t understand that.

    Slight changes (as suggested by ypercube above) work

    SELECT region, name, population FROM bbc x
      WHERE population >= ALL
        (SELECT population FROM bbc y
            WHERE y.region=x.region
              AND population IS NOT NULL)
    

    This query

    SELECT region, name, population FROM bbc x
      WHERE population is null
    

    Returns a row. Not sure why population should be nullable, but didn’t take a good look at the rest of it. Otherwise, the query should work fine without the >0

    Also, this is different from the greatest-n-per-group. In that problem you seek to find the top N items instead of just the top one.

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

Sidebar

Related Questions

So I have been intrigued by the ?? operator, but have still been unable
I have been using TortoiseSVN for some time and I really like it. I
Have been trying to fix this problem I have here. Basically I have a
I have been perpetually intrigued by test-driven development, but I can never follow through
have been searching for a mod operator in ios, just like the % in
I have a 32-bit Windows/Qt application using Postgres plugin. Recently, I've been intrigued to
I have been working with warehousing for a while now. I am intrigued by
have been sitting with this problem for a few days and really can't seem
Have been trying to encrypt an xml file to a string so that I
Have been working on this question for a couple hours and have come close

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.