Watch 10 video solutions for Big Countries, a easy level problem involving Database. This walkthrough by Learn With Chirag has 11,117 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: World
+-------------+---------+ | Column Name | Type | +-------------+---------+ | name | varchar | | continent | varchar | | area | int | | population | int | | gdp | bigint | +-------------+---------+ name is the primary key (column with unique values) for this table. Each row of this table gives information about the name of a country, the continent to which it belongs, its area, the population, and its GDP value.
A country is big if:
3000000 km2), or25000000).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.
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 | 100990000000 | +-------------+-----------+---------+------------+--------------+ Output: +-------------+------------+---------+ | name | population | area | +-------------+------------+---------+ | Afghanistan | 25500100 | 652230 | | Algeria | 37100000 | 2381741 | +-------------+------------+---------+
Problem Overview: The World table contains country data with columns name, population, and area. You need to return countries considered "big"—those with area >= 3000000 or population >= 25000000. The result should include the country's name, population, and area.
Approach 1: SQL Query for Big Countries (Time: O(n), Space: O(1))
The direct solution uses a SELECT query with a WHERE clause that filters rows based on the two conditions. The database engine scans each row of the World table and returns those where either the population threshold or the area threshold is met. This is essentially a linear table scan with conditional filtering. The key insight is that SQL filtering handles the logic declaratively—no loops or extra data structures required. This approach is the expected solution for a database problem and mirrors how production systems query large datasets.
Approach 2: Iterative Filtering (Time: O(n), Space: O(1))
If the dataset is loaded into memory (for example as objects or arrays in Python, Java, or C++), you can solve the problem with a simple iteration. Traverse each country record and check the two conditions: area >= 3000000 or population >= 25000000. When a record satisfies either condition, append it to the result list. This approach mirrors the SQL logic but implements it programmatically using a loop and conditional checks. It commonly appears when practicing array traversal or implementing filtering logic outside a database environment.
Each iteration performs constant-time comparisons, so the algorithm runs in linear time relative to the number of rows. No additional memory is required beyond the output container, keeping auxiliary space constant.
Recommended for interviews: The SQL filtering approach is the expected answer because the problem is tagged under SQL and focuses on writing a clean query with conditional selection. Interviewers typically want to see a precise WHERE clause with logical operators. Understanding the iterative filtering version still helps—it demonstrates the same logic implemented at the application layer and reinforces the underlying algorithmic idea of scanning and filtering records.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| SQL Query with WHERE Filter | O(n) | O(1) | Best when querying directly from a database table using SQL |
| Iterative Filtering (Loop + Condition) | O(n) | O(1) | When data is loaded into memory as arrays/objects in application code |