Screen Link:
https://app.dataquest.io/m/463/joining-data-in-sql/8/challenge-complex-query-with-joins-and-subqueries
My Code:
SELECT facts.name AS country, SUM(cities.population) AS urban_pop, facts.population AS total_pop,(cities.population/facts.population) AS urban_pct
FROM facts
WHERE facts.name > (.5*facts.population)
--INNER JOIN (
--SELECT * FROM facts
--WHERE .5*facts.population)
--cities ON cities.facts_id = facts.id;
INNER JOIN (
SELECT *
FROM cities
WHERE SUM(cities.population) AS urban_pop
)
cities ON cities.facts_id = facts.id;
What I expected to happen:
I wasn’t able to solve this without looking at the hint and then the answer. I don’t understand the thought process behind the construction of the subquery. In particular:
- In the subquery SELECT statement, why is facts_id selected along with SUM(population) urban_pop ? Doesn’t the specific population column need to be specified, or is it because the table for the subquery is cities, we are clearly referring to cities.population? Also, why facts_id?
- I also don’t understand the use of “GROUP BY 1” in the subquery. What role is this playing?
- I understand my query is incorrect, but I am not sure why this doesn’t run and outputs the error below.
What actually happened:
(sqlite3.OperationalError) no such column: cities.population
[SQL: SELECT facts.name AS country, SUM(cities.population) AS urban_pop, facts.population AS total_pop,(cities.population/facts.population) AS urban_pct FROM facts WHERE facts.name > (.5*facts.population) --INNER JOIN ( --SELECT * FROM facts --WHERE .5*facts.population) --cities ON cities.facts_id = facts.id; INNER JOIN ( SELECT * FROM cities WHERE SUM(cities.population) AS urban_pop ) cities ON cities.facts_id = facts.id;]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
Run Code
Thank you!