Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLZOO- using GROUPBY to find the largest country in a continent; is this possible?

I'm working on a practice problem from SQLZOO, and am not sure why the solution I'm trying doesn't work as it makes sense to me.

This is the format of the table::

 -------------------------------------------------------------
|     name      continent    area    population       gdp     |
|-------------------------------------------------------------|
| Afghanistan     Asia      652230    25500100    20343000000 |
| .                                                           |
| .                                                           |
| .                                                           |
|                                                             |
 -------------------------------------------------------------

The question is the following:

Find the largest country (by area) in each continent, show the continent, the name and the area.

Here is the way I was thinking to solve it:

SELECT continent, name, area 
  FROM world
 WHERE name IN (SELECT continent, name, MAX(area) 
                  FROM world 
                 GROUP BY continent);

I know this doesn't work, but why not? It seems like the nested SELECT statement is finding the country with the MAX area per continent, is it not?

The actual solution for this is something like follows:

SELECT continent, name, area 
  FROM world x
 WHERE area >= ALL
    (SELECT area 
       FROM world y
      WHERE y.continent=x.continent
        AND area>0)

But this seems like a complicated way of coming up with it;; is this way makes the most sense? Any ideas are appreciated

Thank you in advance!!

like image 543
SAtt Avatar asked May 24 '18 19:05

SAtt


People also ask

Which countries are big by population and area?

Two ways to be big: A country is big if it has an area of more than 3 million sq km or it has a population of more than 250 million. Show the countries that are big by area or big by population. Show name, population and area. USA and China are big in population and big by area.

How do you find the population of a country in millions?

Show the name and population in millions for the countries of the continent 'South America'. Divide the population by 1000000 to get population in millions. Two ways to be big: A country is big if it has an area of more than 3 million sq km or it has a population of more than 250 million.

How to select all countries with GDP = null?

SELECT name FROM world WHERE gdp > (SELECT MAX (gdp) FROM world WHERE continent = 'Europe' OR gdp > NULL); You only need to use the OR statement as some countries come with gdp = NULL and that's what make your code error.

How to display the two largest points values grouped by team?

We can use the following syntax to display the two largest points values grouped by team: #display two largest points values grouped by team df.groupby('team') ['points'].nlargest(2) team A 2 34 1 29 B 7 36 8 34 Name: points, dtype: int64


2 Answers

While at a quick glimpse this query seems works

SELECT continent, name, area 
  FROM world
 WHERE area IN (SELECT MAX(area) 
                  FROM world 
                 GROUP BY continent);

Demo 1

considering the current data, some issues would raise while some other new records added such as in the demo below. Rather than the above prefer this one :

SELECT w1.continent, name, w1.area 
  FROM world AS w1
  JOIN (SELECT continent, MAX(area) AS area
          FROM world 
         GROUP BY continent) AS w2
    ON w1.continent = w2.continent
   AND w1.area = w2.area

Demo 2

like image 162
Barbaros Özhan Avatar answered Oct 10 '22 20:10

Barbaros Özhan


select A.continent, W.name, A.area
from
(select continent, max(area) as area from world group by continent)A, world W
where
A.continent = W.continent
and
A.area = W.area
like image 43
Shilpa Avatar answered Oct 10 '22 20:10

Shilpa