Skip to content

LeetCode 595: Big Countries

A clear SQL guide for finding countries with either large area or large population.

Problem Restatement

We are given a table called World.

Each row contains information about one country.

A country is considered big if at least one of these conditions is true:

  1. Its area is at least 3,000,000.
  2. Its population is at least 25,000,000.

We need to return the name, population, and area of all big countries. The result may be returned in any order.

Table

World

ColumnTypeMeaning
namevarcharCountry name
continentvarcharContinent name
areaintCountry area
populationintCountry population
gdpbigintCountry GDP

name is the primary key.

Example

Input:

namecontinentareapopulationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748283174112960000000
AlgeriaAfrica238174137100000188681000000
AndorraEurope468781153712000000
AngolaAfrica124670020609294100990000000

Output:

namepopulationarea
Afghanistan25500100652230
Algeria371000002381741

Afghanistan qualifies because its population is at least 25,000,000.

Algeria qualifies for the same reason.

The other countries in the sample do not satisfy either condition.

First Thought: Select All Countries

A first query might be:

SELECT name, population, area
FROM World;

This returns every country.

But the problem only asks for big countries, so we need a WHERE clause.

Key Insight

The problem gives two independent conditions.

A country qualifies if either condition is true:

area >= 3000000

or:

population >= 25000000

Because satisfying either condition is enough, we combine them with OR.

Algorithm

  1. Read rows from World.
  2. Keep countries where area >= 3000000.
  3. Also keep countries where population >= 25000000.
  4. Return only name, population, and area.

Correctness

For any country with area at least 3,000,000, the area >= 3000000 condition is true, so the query includes it.

For any country with population at least 25,000,000, the population >= 25000000 condition is true, so the query includes it.

For any country that satisfies neither condition, both sides of the OR expression are false, so the query excludes it.

Therefore, the query returns exactly the countries defined as big.

Complexity

Let:

n = number of rows in World
MetricValueWhy
TimeO(n)The table is scanned and each row is filtered
SpaceO(1)No extra structure is needed apart from the result

The database may use indexes if available, but a simple scan is enough conceptually.

Implementation

SELECT
    name,
    population,
    area
FROM World
WHERE area >= 3000000
   OR population >= 25000000;

Code Explanation

This selects the required columns:

SELECT
    name,
    population,
    area

This reads from the table:

FROM World

This condition keeps countries with large area:

area >= 3000000

This condition keeps countries with large population:

population >= 25000000

The OR means a country only needs to satisfy one of the two conditions:

WHERE area >= 3000000
   OR population >= 25000000;

Testing

Sample data:

CREATE TABLE World (
    name VARCHAR(255) PRIMARY KEY,
    continent VARCHAR(255),
    area INT,
    population INT,
    gdp BIGINT
);

INSERT INTO World (name, continent, area, population, gdp) VALUES
('Afghanistan', 'Asia', 652230, 25500100, 20343000000),
('Albania', 'Europe', 28748, 2831741, 12960000000),
('Algeria', 'Africa', 2381741, 37100000, 188681000000),
('Andorra', 'Europe', 468, 78115, 3712000000),
('Angola', 'Africa', 1246700, 20609294, 100990000000);

Query:

SELECT
    name,
    population,
    area
FROM World
WHERE area >= 3000000
   OR population >= 25000000;

Expected result:

namepopulationarea
Afghanistan25500100652230
Algeria371000002381741

Additional test cases:

CaseExpected behavior
area = 3000000Included
population = 25000000Included
Large area but small populationIncluded
Small area but large populationIncluded
Both conditions falseExcluded