Can we think of Common Table Expressions as temporary Dataframes and Subqueries as variables?

Hello everyone.

It might be a stupid question - I’m in my initial stages of my SQL journey - but I was wondering if, applying, Python/Pandas logic to SQL we could see Common Table Expressions as Dataframes and Subqueries as variables ?

For instance, I’ve just finished the challenge of the Joining Data In SQL (Part 8 - Complex Query with Joins and Subqueries) and I got along with this code :

WITH popAlternative AS
( SELECT f.name AS country, f.population total_pop, c.population PopVille FROM facts f
INNER JOIN cities c ON c.facts_id = f.id
GROUP BY country, total_pop, PopVille)

SELECT country, SUM(PopVille) AS urban_pop,total_pop,CAST(SUM(PopVille) AS FLOAT) / total_pop AS urban_pct
FROM popAlternative
GROUP BY country
HAVING urban_pct > 0.5
ORDER BY urban_pct

Is it a good analogy or should I ditch it because this way of thinking might cause some troubles in the long run ?
Because my code is way different compared to the Dataquest solution :

SELECT
f.name country,
c.urban_pop,
f.population total_pop,
(c.urban_pop / CAST(f.population AS FLOAT)) urban_pct
FROM facts f
INNER JOIN (
SELECT
facts_id,
SUM(population) urban_pop
FROM cities
GROUP BY 1
) c ON c.facts_id = f.id
WHERE urban_pct > .5
ORDER BY 4 ASC;

Any opinions ? Thanks in advance.

Thibaut