Back to blog
Dec 30, 2024
4 min read

Big Countries

Find the name, population, and area of countries with an area of at least three million square kilometers or a population of at least twenty-five million.

Difficulty: Easy | Acceptance: 68.50% | Paid: No Topics: Database

Table: World

+-------------+-----------+ | Column Name | Type | +-------------+-----------+ | name | varchar | | continent | varchar | | area | int | | population | int | | gdp | bigint | +-------------+-----------+ name is the primary key for this table.

A country is big if:

it has an area of at least three million (i.e., 3000000), or it has a population of at least twenty-five million (i.e., 25000000).

Write a solution to find the name, population, and area of the big countries.

Return the result table in any order.

The result format is in the following example.

Examples

Example 1:

Input:
World table:
+-------------+-----------+---------+------------+--------------+
| 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   | 100360000000 |
+-------------+-----------+---------+------------+--------------+
Output:
+-------------+------------+---------+
| name        | population | area    |
+-------------+------------+---------+
| Afghanistan | 25500100   | 652230  |
| Algeria     | 37100000   | 2381741 |
+-------------+------------+---------+
Explanation:
Afghanistan has a population of 25500100, which is greater than 25000000.
Algeria has a population of 37100000, which is greater than 25000000.

Constraints

There are no specific constraints listed for this problem other than the table schema.

Approach 1: Direct Filtering

Intuition We can directly filter the World table using a WHERE clause with an OR condition to select countries that satisfy either the area or the population requirement.

Steps

  • Select the columns name, population, and area from the World table.
  • Filter the rows where area is greater than or equal to 3,000,000 (3×10⁶).
  • Alternatively, filter the rows where population is greater than or equal to 25,000,000 (2.5×10⁷).
  • Combine these conditions using the OR operator.
python
class Solution:
    def query(self) -> str:
        return 'SELECT name, population, area FROM World WHERE area >= 3000000 OR population >= 25000000'

Complexity

  • Time: O(N), where N is the number of rows in the World table. The database engine scans the table once.
  • Space: O(1), excluding the space required for the output result.
  • Notes: This is the most efficient approach as it requires a single pass over the data.

Approach 2: Set Union

Intuition We can treat the problem as finding the union of two sets: countries with large area and countries with large population. We can use the UNION operator to combine the results of two separate queries.

Steps

  • Select name, population, and area from World where area >= 3000000.
  • Select name, population, and area from World where population >= 25000000.
  • Use UNION to combine these two result sets, which automatically removes duplicate rows (countries that meet both criteria).
python
class Solution:
    def query(self) -> str:
        return 'SELECT name, population, area FROM World WHERE area >= 3000000 UNION SELECT name, population, area FROM World WHERE population >= 25000000'

Complexity

  • Time: O(N) or O(N log N), depending on the database implementation. UNION involves deduplication, which typically requires sorting or hashing.
  • Space: O(N) in the worst case to store the intermediate results for the union operation.
  • Notes: While logically correct, this approach is generally less efficient than the single WHERE clause with OR because it may require scanning the table twice or performing additional processing for deduplication.