0

I wanna pick cities from worldcity by these conditions:

  • the population of the city exceeds the population of the most populous city in the Filipina. (Meaning: for example city X is the city in the Filipina with the most population. We want to list the cities with a population greater than city X), AND
  • there is a city in the United States (uscity) with the same city name as the selected cities

``

worldcity.colomns = ['city', 'lat','lng','country','iso2','iso3','capital','population','id']

uscity.columns = ['city', 'state_id', 'state_name', 'county_fips', 'county_name',
   'lat', 'lng', 'population', 'density', 'source', 'military',
   'incorporated', 'timezone', 'ranking', 'zips', 'id']

query = """
SELECT DISTINCT
    city
FROM
    worldcity as w
inner join
      uscity as u
on
      w.city = u.city
WHERE w.population >= (MAX)population IN
    (SELECT
        population
    FROM worldcity
    WHERE
        country = 'Filipina';
    )
"""
sql_run(query)

OperationalError: near "population": syntax error

1
  • select max(population) from (select population...) Commented Feb 25, 2022 at 23:27

2 Answers 2

0

Adjust the WHERE clause to place the MAX inside the subquery:

...
WHERE w.population > (
    SELECT MAX(population)
    FROM worldcity 
    WHERE country = 'Filipina'
)
Sign up to request clarification or add additional context in comments.

2 Comments

it's still error. PandaSQLException: (sqlite3.OperationalError) ambiguous column name: city
That's a different error. In your SELECT clause qualify which table to use for city: w.city or u.city. Since you inner join both should be the same, so pick either.
0

I slightly changed the answer you wrote

Maybe this will help because I've tried it and it works

You can change ( > to >=) if you want to include the city with the highest population in the Philippines

query = """
SELECT DISTINCT w.city
FROM 
      worldcity as w 
inner join
      uscity as u
on 
      w.city = u.city
WHERE w.population > (SELECT MAX(population)
                      FROM worldcity 
                      WHERE country = 'Philippines')
ORDER BY w.city
"""
sql_run(query)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.