I'm a beginner at SQL and this is the question I have been asked to solve:
Say that a big city is defined as a
placeof typecitywith a population of at least 100,000. Write an SQL query that returns the scheme(state_name,no_big_city,big_city_population)ordered bystate_name, listing those states which have either (a) at least five big cities or (b) at least one million people living in big cities. The columnstate_nameis thenameof thestate,no_big_cityis the number of big cities in the state, andbig_city_populationis the number of people living in big cities in the state.
Now, as far as I can see, the following query returns correct results:
SELECT state.name AS state_name
     , COUNT(CASE WHEN place.type = 'city' AND place.population >= 100000 THEN 1 ELSE NULL END) AS no_big_city
     , SUM(CASE WHEN place.type = 'city' AND place.population >= 100000 THEN place.population ELSE NULL END) AS big_city_population
FROM state
JOIN place
ON state.code = place.state_code
GROUP BY state_name
    HAVING
        COUNT(CASE WHEN place.type = 'city' AND place.population >= 100000 THEN 1 ELSE NULL END) >= 5 OR
        SUM(CASE WHEN place.type = 'city' AND place.population >= 100000 THEN place.population ELSE NULL END) >= 1000000
ORDER BY state_name;
However, the two aggregate functions used in the code appear twice. MY question: is there any way of making this code duplication disappear preserving functionality?
To be clear, I have already tried using the alias, but I just get a "column does not exist" error.
 
     
     
     
    