-1

Question: Given the CITY and COUNTRY tables, query the sum of the populations of all cities where the CONTINENT is 'Asia'.

City table contains fields: CountryCode, Population Country table contains fields: Code, Continent (CITY.CountryCode and COUNTRY.Code are matching key columns.)

I tried the following query: (I know this can be solved using Inner join)

Select sum(city.population) from city
where city.countrycode in (Select code from Country where continent = 'Asia')

Hacker rank gives following error: ERROR at line 3: ORA-00933: SQL command not properly ended

6
  • Your syntax look right, so what's the question - how to do it with an inner join? Commented Jun 8, 2016 at 13:17
  • Looks fine. Inner join would be fine too. And EXISTS. Commented Jun 8, 2016 at 13:18
  • Hackerrank gives following error: ERROR at line 3: ORA-00933: SQL command not properly ended. Also, @jarlh when do we use in and when do we use exist operation? Commented Jun 8, 2016 at 13:22
  • 1
    Maybe Hacker Rank requires an ending semicolon? if this query were run in SQL developer it looks like it would work. Commented Jun 8, 2016 at 13:23
  • Yes, ; worked. This is stupid. I have an interview with Amazon data engineer role and have a sql test. Syntax for mysql/ oracle (ex. AS alias does not work in hackerrank portal) does not seem consistent on different forums. Any suggestion? Commented Jun 8, 2016 at 13:27

6 Answers 6

2

This is the right code

Select sum(city.population) 
from city 
inner join country on country.code = city.countrycode
where country.continent= 'Asia';
Sign up to request clarification or add additional context in comments.

Comments

1

Do you need a semi colon?

Something along these lines......

Per city..........

SELECT City.Name, SUM(City.Population)
FROM City INNER JOIN Country ON Country.Code = City.CountryCode
WHERE
  Country.Continent = 'ASIA'
GROUP BY 
  City.Name;

Per Country & City

SELECT Country.Name, City.Name, SUM(City.Population)
FROM City INNER JOIN Country ON Country.Code = City.CountryCode
WHERE
  Country.Continent = 'ASIA'
GROUP BY 
  Country.Name, City.Name;

Just the Total for ASIA

SELECT SUM(City.Population)
FROM City INNER JOIN Country ON Country.Code = City.CountryCode
WHERE
  Country.Continent = 'ASIA';

4 Comments

I know this. My question is what is wrong with the query I have written?
Also, what works faster: using the inner join or the query I have written? And why?
That would depend on the amount of data in the tables. Stick with the INNER JOIN, it will scale better as your tables grow. Your original query will have to scan through the country table to find the continent....Imagine if you have a lot of countries in there.
From what I've read: it used to be that the "in" syntax would result in slower execution. Since Oracle 10 (I think) the wise folks at Oracle figured out that the "in" syntax is equivalent to a join and the optimizer now does that for you. The performance should be the same. Which you could check for yourself by experimentation.
1

What we are doing here is querying the city table and finding all matches in the country table by using the identifying field in both those tables:

country.code in your country table

city.countrycode in your city table

Whenever country.code = city.countrycode and country.continent= 'Asia', you will be returned the sum of that population.

Select sum(city.population) 
from city 
inner join country on country.code = city.countrycode
where country.continent= 'Asia';

I also recommend you select the city the population count belongs to:

Select city.name, sum(city.population) 
from city 
inner join country on country.code = city.countrycode
where country.continent= 'Asia';

1 Comment

The 2nd sample won't work, because it mixes agreggate with individual results without a group by clause.
0

Use ; at the end of the code.

Select sum(city.population) from city
where city.countrycode in (Select code from Country where continent = 'Asia');

Comments

0
SELECT SUM(City.POPULATION) FROM City JOIN Country ON city.countrycode=country.code WHERE country.continent='Asia'

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1
select sum(c.population) from city c,country cn
where cn.continent='Asia' and c.countrycode=cn.code;

2 Comments

Please do not post unformatted code, without explaining why this is better than the other answers here....
To clarify @Luuk’s feedback: Even if it’s formatted—as it is now, per my edit—you should still offer an explanation of what your code does and, since there are established answers, why you prefer your approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.