Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the countries with a GDP greater than a group of countries

Tags:

sql

I'm attempting question 5 of SQLZoo's SELECT within SELECT tutorial

Which countries have a GDP greater than any country in Europe? [Give the name only.]

This is my solution, which is incorrect but I don't understand why.

SELECT name  
  FROM world  
 WHERE gdp > ALL (SELECT gdp FROM world WHERE continent ='Europe')

What am I doing incorrectly?

like image 890
kreativred Avatar asked May 18 '13 09:05

kreativred


People also ask

Can you compare GDP of different countries?

Summary. Since GDP is measured in a country's currency, in order to compare different countries' GDPs, we need to convert them to a common currency. One way to compare different countries' GDPs is with an exchange rate, the price of one country's currency in terms of another. GDP per capita is GDP divided by population ...

How do you find the GDP of a country?

Gross Domestic Product data can be found in the National Accounts dataset portal, and in the Data Tables tab of the International Financial Statistics dataset portal.


2 Answers

select name, gdp 
from world x 
where gdp > all (select gdp from world y where continent = "Europe" and gdp > 0)

you were missing a NULL value check because in case of null value unknown result will be fetched, see ref: http://dev.mysql.com/doc/refman/5.5/en/all-subqueries.html

world table might have null values for some European countries when you tested your query therefore it is good to put a check to avoid null values.

like image 99
Vineet Sajwan Avatar answered Sep 27 '22 22:09

Vineet Sajwan


SELECT name 
  FROM world 
 WHERE gdp > (SELECT MAX(gdp) FROM world WHERE continent = 'Europe')

Using the MAX aggregate function returns the highest GDP from the sub-query which is what you want to compare with the outer query.

like image 41
Lioness818 Avatar answered Sep 27 '22 22:09

Lioness818