0

I was asked this question in an interview and I could not get my mind around it, and now it is bothering me a lot!

If you have a table CANADA(PROVINCE, CITY, POPULATION). - Population is correlated with the population of one city. - There are multiple cities in a province.

Question: Write a query that would list the total population of each Province.

I am a beginner when it comes to SQL related questions. Knoweldge of SQL was not in my job description, but they just wanted to know if I could solve it.

I came up with this: SELECT PROVINCE, POPULATION WHERE CANADA GROUP BY PROVINCE

However I did not know how to move forward with this. I would like to know of I am going the right direction please!

1
  • There are a lot of free SQL tutorials on the Internet and I would recommend you go through one. That will be much more helpful than just having someone give you the answer. Commented Mar 9, 2019 at 22:27

1 Answer 1

1

You need to include the aggregate operation to get the sum of population. So:

SELECT Province, SUM(Population) AS PopulationSum FROM CANADA GROUP BY Province

In english it would be, I want the sum of the population column, by province from the Canada table.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.