1

I have this query that works, but it returns information for all cities and I only want to return based on the max population in the city for each row for one country but aggregate functions can't be used in the where clause. How can I limit my results to one per country?

SELECT lab6.country.name, max(lab6.city.population) AS largest_pop 
  FROM lab6.country, lab6.city 
 WHERE lab6.country.country_code = lab6.city.country_code 
 GROUP BY lab6.country.name, lab6.city.name"
6
  • What RDBMS you are using? RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, etc...
    – John Woo
    Commented Mar 12, 2013 at 16:02
  • This is the third time this question has been asked today, coming out of the same homework assignment. I'm glad teachers are aware of SO, but maybe the students should be better prepared and instructed so that they can answer it themselves. Commented Mar 12, 2013 at 16:38
  • I'm asking for help to understand, not for someone to do a homework assignment for me. Commented Mar 12, 2013 at 17:12
  • You really should provide example values and an example result to make yourself clear with a question like that. Too many ways to misunderstand and waste time. Commented Mar 16, 2013 at 0:37
  • possible duplicate of Query Returning Too Many Results? Commented Mar 16, 2013 at 0:46

2 Answers 2

1

PostgreSQL supports window functions that you can take advantage with.

SELECT  countryName, cityName, largest_pop
FROM
        (
            SELECT  a.name countryName, 
                    b.name cityName, 
                    b.population AS largest_pop,
                    DENSE_RANK() OVER (PARTITION BY a.name 
                                        ORDER BY b.population DESC) rn
            FROM    lab6.country a, lab6.city b 
            WHERE   a.country_code = b.country_code 
        ) x
WHERE   rn = 1
0

Maybe I'm misunderstanding but do you just want to return the largest city in each country?

If so, you simply can group by country, instead of by country and city. You'll need to include the attribute that identifies a country, and the name of that country in your GROUP BY statement. Your query will end up looking like:

SELECT lab6.country.name AS cName, max(lab6.city.population) AS largest_pop 
FROM lab6.country, lab6.city 
WHERE lab6.country.country_code = lab6.city.country_code 
GROUP BY lab6.country.country_code, lab6.country.name

If you want to also include the name of the largest city, you'll first need to decide what to do if there are multiple largest cities (countries where there are two or more cities with the same, largest, population). I'm going to assume you're okay with including them all. In that case, you can simply do a sub-query in your FROM clause, joined on cities with the same population:

SELECT lc.cName, lab6.city.name, lc.largest_pop
FROM (
      SELECT lab6.country.country_code AS cCode
             lab6.country.name AS cName, 
             max(lab6.city.population) AS largest_pop 
      FROM lab6.country, lab6.city 
      WHERE lab6.country.country_code = lab6.city.country_code 
      GROUP BY lab6.country.country_code, lab6.country.name
     ) AS lc
     JOIN lab6.city ON lc.cCode = lab6.city.country_code 
WHERE lab6.city.population = lc.largest_pop
5
  • Yes, I'm wanting to return the largest city but the city names are in a different table then the countries. Example lab6.city.name and lab6.country.name Commented Mar 12, 2013 at 16:25
  • so if I insert lab6.city.name inside the SELECT statement I have to place it in the GROUP BY clause. Commented Mar 12, 2013 at 16:27
  • So you also want the name of the largest city included, not just the population? You might want to include that specific bit of information in your question.
    – Wilduck
    Commented Mar 12, 2013 at 16:27
  • Yes and that's in the lab6.city table, but I'm not sure how to include that without having to place it in the GROUP BY which causes way too many results Commented Mar 12, 2013 at 16:28
  • Okay, I've put something together that should work. See my edit.
    – Wilduck
    Commented Mar 12, 2013 at 16:35

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.