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:
- Its area is at least
3,000,000. - 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
| Column | Type | Meaning |
|---|---|---|
name | varchar | Country name |
continent | varchar | Continent name |
area | int | Country area |
population | int | Country population |
gdp | bigint | Country GDP |
name is the primary key.
Example
Input:
| name | continent | area | population | gdp |
|---|---|---|---|---|
| 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 |
Output:
| name | population | area |
|---|---|---|
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
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 >= 3000000or:
population >= 25000000Because satisfying either condition is enough, we combine them with OR.
Algorithm
- Read rows from
World. - Keep countries where
area >= 3000000. - Also keep countries where
population >= 25000000. - Return only
name,population, andarea.
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| Metric | Value | Why |
|---|---|---|
| Time | O(n) | The table is scanned and each row is filtered |
| Space | O(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,
areaThis reads from the table:
FROM WorldThis condition keeps countries with large area:
area >= 3000000This condition keeps countries with large population:
population >= 25000000The 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:
| name | population | area |
|---|---|---|
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
Additional test cases:
| Case | Expected behavior |
|---|---|
area = 3000000 | Included |
population = 25000000 | Included |
| Large area but small population | Included |
| Small area but large population | Included |
| Both conditions false | Excluded |