I have been asked to solve this question:
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; The lengthy aggregate function expressions appear twice.
Is there any way to avoid code duplication while preserving functionality?
I have already tried using the alias, but I just get a "column does not exist" error.
no_big_city, does not exist...HAVINGclause only accepts input column names. I added the relevant quote from the manual below.