SQL, choosing maximum values from another table

Aleksander Simmersholm

This one I've been spending the last hours on. Still haven't found an answer so I figured I'd ask. I have two tables:

Country - Code, Name, Populaion.
City - CountryCode, Name, Population. 

The Foreign key is the CountryCode which response to country.Code.

I'm trying to find the most populated city in each country, where the output is the name of the country and the city. I know this could be done with Max(), but I'm having a struggle limiting my table to showing all the countries name and showing onle the name of the most populated city.

SELECT country.name, city.name, MAX(city.Population) 
FROM city
LEFT JOIN country
ON city.CountryCode=Country.Code
GROUP BY city.name, country.name, city.population
ORDER BY city.population DESC;

This only gives me all the countries and all the cities. Could anyone help me narrow it down so it only shows every countries name but with their largest city?

Gurwinder Singh

You want to first the max population in each country and then join it with the city table to know which city it belongs to. After that, join it with the country table to get the required result.

select co.name,
    ct.name,
    ct.population
from (
    select c1.*
    from city c1
    join (
        select countryCode,
            max(population) population
        from city
        group by countryCode
        ) c2 on c1.countryCode = c2.countryCode
        and c1.population = c2.population
    ) ct
join country co on ct.countryCode = co.code;

Another way of finding max in group using left join:

select co.name,
    ct.name,
    ct.population
from (
    select c1.*
    from city c1
    left join city c2 on c1.countryCode = c2.countryCode
        and c1.population < c2.population
    where c2.countryCode is null
    ) ct
join country co on ct.countryCode = co.code;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sort SQL table with values from another table

From Dev

Sql to select the maximum datetime row keeping values from another row

From Java

SQL to copy values from one table to another

From Dev

Updating columns values from another table SQL

From Dev

SQL Query For Values From Another Table

From Dev

Populate a column in SQL with values from another table

From Dev

SQL - Update table values from values in another table

From Dev

Update sql table daily with values from another table

From Dev

SQL: How to SELECT different values from a table in another table?

From Dev

SQL: How to SELECT different values from a table in another table?

From Dev

How to insert from one table into another with extra values in SQL?

From Dev

Insert in SQL table using select with column values from another select

From Dev

How to create sql query to insert values from another table?

From Dev

Replacing NAs in column with values from another table SQL Server

From Dev

SQL - Selecting a column from another table twice with different values

From Dev

SQL SELECT - Return new columns populated with values from another table

From Dev

How can I create a SQL table from another table without copying any values from the old table

From Dev

How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

From Java

SQL/MS Access: Adding a automatically updating field in SQL table with sum of values from another table

From Dev

MS SQL: INSERT values from another table and values from constant data

From Dev

MS SQL: INSERT values from another table and values from constant data

From Dev

SQL Server 2008 Preserve rows from table with non-matching values from another table

From Dev

Update Table by getting maximum value from another table

From Dev

MySQL - Update table values from another table

From Dev

Create a table with unique values from another table

From Dev

inserting table values from one table to another

From Dev

Setting values in a table from another table

From Dev

SQL Select - inserting values if not exist from another table grouping by another value

From Dev

SQL Select - inserting values if not exist from another table grouping by another value

Related Related

  1. 1

    Sort SQL table with values from another table

  2. 2

    Sql to select the maximum datetime row keeping values from another row

  3. 3

    SQL to copy values from one table to another

  4. 4

    Updating columns values from another table SQL

  5. 5

    SQL Query For Values From Another Table

  6. 6

    Populate a column in SQL with values from another table

  7. 7

    SQL - Update table values from values in another table

  8. 8

    Update sql table daily with values from another table

  9. 9

    SQL: How to SELECT different values from a table in another table?

  10. 10

    SQL: How to SELECT different values from a table in another table?

  11. 11

    How to insert from one table into another with extra values in SQL?

  12. 12

    Insert in SQL table using select with column values from another select

  13. 13

    How to create sql query to insert values from another table?

  14. 14

    Replacing NAs in column with values from another table SQL Server

  15. 15

    SQL - Selecting a column from another table twice with different values

  16. 16

    SQL SELECT - Return new columns populated with values from another table

  17. 17

    How can I create a SQL table from another table without copying any values from the old table

  18. 18

    How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

  19. 19

    SQL/MS Access: Adding a automatically updating field in SQL table with sum of values from another table

  20. 20

    MS SQL: INSERT values from another table and values from constant data

  21. 21

    MS SQL: INSERT values from another table and values from constant data

  22. 22

    SQL Server 2008 Preserve rows from table with non-matching values from another table

  23. 23

    Update Table by getting maximum value from another table

  24. 24

    MySQL - Update table values from another table

  25. 25

    Create a table with unique values from another table

  26. 26

    inserting table values from one table to another

  27. 27

    Setting values in a table from another table

  28. 28

    SQL Select - inserting values if not exist from another table grouping by another value

  29. 29

    SQL Select - inserting values if not exist from another table grouping by another value

HotTag

Archive