Big Countries - Problem

In the world of geopolitics and data analysis, we often need to identify "big countries" - nations that stand out due to their massive size or population.

You are given a database table called World containing information about various countries:

Column NameType
namevarchar
continentvarchar
areaint
populationint
gdpbigint

A country is considered "big" if it meets at least one of these criteria:

  • Has an area of at least 3,000,000 km² (like Russia, Canada, or USA)
  • Has a population of at least 25,000,000 people (like China, India, or Indonesia)

Goal: Write a SQL query to find the name, population, and area of all big countries. The result can be returned in any order.

Input & Output

example_1.sql — Basic World Data
$ Input: World table: | name | continent | area | population | gdp | |-------------|-----------|---------|------------|-------------| | Afghanistan | Asia | 652000 | 25500100 | 20343000000 | | Albania | Europe | 28000 | 2831741 | 12960000000 | | Algeria | Africa | 2381000 | 37100000 | 188681000000| | Russia | Europe | 17100000| 146934000 | 1483000000000|
Output: | name | population | area | |-------------|------------|----------| | Afghanistan | 25500100 | 652000 | | Algeria | 37100000 | 2381000 | | Russia | 146934000 | 17100000 |
💡 Note: Afghanistan qualifies due to population >= 25M, Algeria qualifies due to population >= 25M, and Russia qualifies for both criteria (area >= 3M km² and population >= 25M). Albania doesn't qualify for either criterion.
example_2.sql — Area-based Selection
$ Input: World table: | name | continent | area | population | gdp | |--------|-----------|---------|------------|------------| | Canada | America | 9970000 | 37742154 | 1736000000000| | Monaco | Europe | 2 | 39244 | 6075000000| | Brazil | America | 8515000 | 212559417 | 1869000000000|
Output: | name | population | area | |--------|------------|----------| | Canada | 37742154 | 9970000 | | Brazil | 212559417 | 8515000 |
💡 Note: Canada qualifies due to area >= 3M km² (and also population >= 25M). Brazil qualifies for both criteria. Monaco doesn't qualify despite being wealthy - it's too small in both area and population.
example_3.sql — Edge Case - Exact Thresholds
$ Input: World table: | name | continent | area | population | gdp | |----------|-----------|---------|------------|------------| | Country1 | Asia | 3000000 | 24999999 | 1000000000| | Country2 | Africa | 2999999 | 25000000 | 2000000000| | Country3 | Europe | 3000001 | 20000000 | 1500000000|
Output: | name | population | area | |----------|------------|----------| | Country1 | 24999999 | 3000000 | | Country2 | 25000000 | 2999999 | | Country3 | 20000000 | 3000001 |
💡 Note: Country1 qualifies with exactly 3M km² area. Country2 qualifies with exactly 25M population. Country3 qualifies with area just over the threshold. All boundary conditions are inclusive.

Visualization

Tap to expand
Big Countries Selection ProcessRussia17M km²146M popChina9.6M km²1.4B popIndia3.3M km²1.4B popJapan0.38M km²125M popSelection Criteria (OR Logic)Area ≥ 3,000,000 km² OR Population ≥ 25,000,000✓ Russia, China, India qualify | ✗ Japan (area too small, but population qualifies!)Result: All countries shown qualify as "Big Countries"!(Japan qualifies due to population ≥ 125M > 25M threshold)
Understanding the Visualization
1
Scan Each Country
Examine every row in the World table systematically
2
Apply OR Logic
Check if area ≥ 3M km² OR population ≥ 25M people
3
Select Winners
Include countries that meet either criterion in results
Key Takeaway
🎯 Key Insight: Use the OR operator to combine multiple criteria efficiently - the database engine will optimize the query execution and return all rows that satisfy at least one condition.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through all rows in the World table

n
2n
Linear Growth
Space Complexity
O(1)

No additional space needed beyond the result set

n
2n
Linear Space

Constraints

  • 1 ≤ number of countries ≤ 500
  • Country names are unique and non-empty
  • 0 ≤ area ≤ 20,000,000 km²
  • 0 ≤ population ≤ 2 × 109
  • Threshold values: area ≥ 3,000,000 OR population ≥ 25,000,000
Asked in
Meta 45 Amazon 38 Google 32 Microsoft 28
89.5K Views
High Frequency
~5 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen