Mysql – List population of provices (SQL Query)

interview-questionMySQLquery

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!

Best Answer

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.