Skip to main content

Big Countries - Solution & Explanation

EasyDatabase8 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

Problem Statement

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:

  • it has an area of at least three million (i.e., 3000000 km2), 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.

 

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 |
+-------------+------------+---------+

Approach Overview

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 1: SQL Query for Big Countries

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.

Code

SQL

Complexity

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.

Try this approach in the editor →

Approach 2: Iterative Filtering Approach

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.

Code

Python

JavaScript

Java

C#

C++

Complexity

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.

Try this approach in the editor →

Approach 3: Default Approach

Code

MySQL

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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.
Space Complexity: O(n) due to storing the result set that meets the criteria.

Iterative Filtering Approach

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.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
SQL Query with WHERE FilterO(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

Video Solution

Big Countries | Leetcode 595 | SQL_50 Study Plan | Crack SQL Interviews in 50 Qs #mysql #leetcodeLearn With Chirag12,575 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Big Countries easy or hard?
Big Countries is classified as an Easy problem on LeetCode with a high acceptance rate. It focuses on basic SQL filtering using logical OR conditions and is commonly used to test fundamental query skills.
How to solve Big Countries in O(n)?
Scan each record exactly once and apply the condition area >= 3000000 OR population >= 25000000. In SQL, this is done using a WHERE clause. In programming languages, iterate through the dataset and append matching countries to the result list.
Big Countries Python or Java solution?
In Python or Java, load the records into a list and iterate through each country. Check if population >= 25000000 or area >= 3000000 and add matching entries to the output list. The logic mirrors the SQL WHERE filtering and runs in O(n) time.
What is the best approach for Big Countries?
The best approach uses a SQL SELECT query with a WHERE clause that filters rows where area >= 3000000 or population >= 25000000. This directly expresses the requirement and runs in O(n) time as the database scans the table once. It is also the solution interviewers expect for a SQL-tagged problem.
What data structure is used in Big Countries?
The SQL version operates directly on a relational table. In language implementations, the data is typically represented as arrays or lists of records (objects or structs) that you iterate through and filter based on conditions.
What is the time complexity of Big Countries?
The time complexity is O(n), where n is the number of rows in the World table. The query or iteration checks each row once and evaluates two constant-time conditions. Space complexity is O(1) aside from the result set returned.
Is Big Countries asked at Google, Amazon, or Meta?
Big Countries is a basic SQL filtering problem commonly used in database interview preparation. Similar questions appear in interviews at companies that assess SQL fundamentals, including large tech companies and data-focused roles.

Ready to solve this problem?

Practice Big Countries with our built-in code editor and test cases.

Practice on FleetCode