Why is CAST placed in denominator ? Why not placed in both numerator and denominator?

Screen Link:

https://app.dataquest.io/m/463/joining-data-in-sql/8/challenge-complex-query-with-joins-and-subqueries

My Code:

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

What I expected to happen:

What actually happened:

Replace this line with the output/error

Why is CAST placed in denominator ? Why not placed in both numerator and denominator?

You can place it in the denominator, or in the numerator, or both.

@the_doctor , Does it matter where CAST is place?

Based on some small experimentation I did, it doesn’t for this particular scenario. As I mentioned above, any of those work for SQLite.

1 Like