r/learnSQL • u/FitShock5083 • Apr 03 '26
Help Me Understand the Last 5% of this Code Please
Hi again all. I'm really making great progress learning SQL, but I have a question regarding subqueries that none of the training modules or books explain and so I'm confused. I (think I) understand the following from the code (see 1- below) ... *** but I don't know what the WHERE name IN on line 4 is doing. Can anyone explain what the WHERE name IN is doing and how it relates to the overall code? Does it relate to the name column called out first in the SELECT portion? **\*
- It is creating a temp_table of capitals whose continent is in North America, South America or Europe and that also have a metro area population above 0 from the countries table.
- It is then looking at each row in the cities table and any that are capitals (matched via the temp_table) are included in the output of name, country_code, city_proper_pop, metroare_pop and city_perc.
- It then orders the output by city_perc in descending order, limited to the first 10 rows.
--------------------------------------------------------------------------------------------------------
SELECT
name, country_code, city_proper_pop, metroarea_pop, city_perc
FROM cities
WHERE name IN
(SELECT capital
FROM countries
WHERE (continent = 'Europe' OR continent LIKE '%America'))
AND metroarea_pop IS NOT NULL
ORDER BY city_perc DESC
LIMIT 10;
6
u/ComicOzzy Apr 04 '26
This subquery returns a list of capitals from the countries table. Think of it as:
That list of capitals is a "derived table" ("temp table" means something a little different).
The query is filtering the
citiestable based on thenamecolumn. It only returns rows where thenameis in the list of capitals returned by the subquery.After the
name IN (<list of capitals>)filter, there is an additional filter onmetroarea_popmaking sure there is a value present. The value does not have to be above 0. It can be any value at all.NULLmeans <unknown>.IS NOT NULLmeans there is a value... any value.1
u/Oh_Another_Thing Apr 03 '26
Hmm, I can give a better answer later, but one of your assumptions is not correct. The query says not null, you think that means above zero and that is not correct. Null dies not mean zero.
If you have a math function, anything at all, F=2X, you can get a value of zero. Null means that the calculation never took place.
Likewise zero is a valid number, a null is where nothing was entered.
1
u/DevilsMathematician Apr 03 '26
It's just a temp table (list) used for filtering the result set.
A single col 'capital' is returned from subquery on the table 'countries' with the applied where cond. Then cities is filtered on name col being in the capital list from subquery.
The syntax for within list is simply "col in (select col from tab)". You can actually put anything in the parenthesis that returns a single col, like a string_split() or similar function.
1
u/Far_Swordfish5729 Apr 03 '26
Carefully count your parentheses and don’t make assumptions about temp tables.
This subquery is functionally a shorthand join that selects nothing from the joined table into the intermediate result set and only serves to limit the left hand table (the from table) to matched rows as an inner join would. In execution plans it’s called a semi join and could also be expressed as an exists clause. It logically executes before the rest of the where clause which acts on the from table columns. The engine will likely do that in memory rather than using an actual temp table (table spool if added to an execution plan automatically) and there is no explicit temp table created.
The rest of what you said is correct.