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.
The task requires selecting names, populations, and areas of countries based on certain criteria: whether the area is at least 3 million or the population is at least 25 million. We can achieve this using a SQL query that utilizes the WHERE clause to filter the specified conditions.
This SQL query selects the name, population, and area columns from the World table. It uses a WHERE clause to filter the records where either the area is greater than or equal to 3 million or the population is greater than or equal to 25 million. The OR operator is used to check if any of the conditions are met. The result consists of records that satisfy at least one of these conditions.
SQL
Time Complexity: O(n) where n is the number of rows in the World table, as all rows potentially need to be scanned once.
Space Complexity: O(n) due to storing the result set that meets the criteria.
In the iterative filtering approach, we simulate the results of the SQL query using an iterative process in various programming languages. We loop through each record, check if a record satisfies either of the conditions, and collect the results accordingly. This is done using simple loops and condition checks.
This Python function takes a list of dictionaries where each dictionary represents a country's information. It iterates over the list and checks if the 'area' is at least 3 million or the 'population' is at least 25 million. If a condition is satisfied, the country is added to the result list.
Python
JavaScript
Java
C#
C++
Time Complexity: O(n), where n is the number of countries.
Space Complexity: O(n), to store the resulting list of countries that meet the criteria.
MySQL
| Approach | Complexity |
|---|---|
| SQL Query for Big Countries | Time Complexity: O(n) where n is the number of rows in the World table, as all rows potentially need to be scanned once. |
| Iterative Filtering Approach | Time Complexity: O(n), where n is the number of countries. |
| Default Approach | — |
| 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 |
Big Countries | Leetcode 595 | SQL_50 Study Plan | Crack SQL Interviews in 50 Qs #mysql #leetcode • Learn With Chirag • 11,117 views views
Watch 9 more video solutions →Practice Big Countries with our built-in code editor and test cases.
Practice on FleetCode