Knowledge Guide
HomeDatabasesSQL Practice Problems

Major Cities

Problem Statement

Table: World
Each row in this table represents a city, detailing its name, the country it's in, its area in square kilometers, and its population.

+-----------------+---------+
| Column Name     | Type    | 
+-----------------+---------+
| city_name       | varchar | 
| country         | varchar |
| area            | int     |  -- in square kilometers
| population      | int     | 
+-----------------+---------+
city_name is the primary key for this table.
This table contains information about the city name, the country it is in, its area in square kilometers, and its population.

Develop a solution to identify the names of major cities that either have an area greater than 3000 square kilometers or a population exceeding 5 million people. The output should be sorted in any order.

Example

Input:

World table: +-----------+---------+-------+------------+ | city_name | country | area | population | +-----------+---------+-------+------------+ | Tokyo | Japan | 6223 | 13929286 | | Delhi | India | 1484 | 11034555 | | Shanghai | China | 6341 | 24256800 | | New York | USA | 783 | 8336817 | | Paris | France | 105 | 2148271 | +-----------+---------+-------+------------+

Expected Output:

+-----------+ | city_name | +-----------+ | Tokyo | | Delhi | | Shanghai | | New York | +-----------+

Tokyo, Shanghai, and New York are identified as major cities either due to their large area or high population.

Try it YourSelf

java
-- TODO: Write your user queries here

Solution

To find major cities based on their area or population, we'll execute SQL queries to filter and retrieve the needed information.

  1. Select City Names: Begin with a SELECT statement to fetch the city_name from the World table.
  2. Apply Conditions: Use a WHERE clause to filter cities either with an area > 3000 or population > 5000000.

SQL Query

Here's the SQL query that addresses this:

SELECT city_name FROM World WHERE area > 3000 OR population > 5000000;

Step by Step Approach

Step 1: Select City Names

Start by selecting the city_name from the World table, which will form the core of our output.

SELECT city_name FROM World

Output After Step 1:

+-----------+ | city_name | +-----------+ | Tokyo | | Delhi | | Shanghai | | New York | | Paris | +-----------+

Step 2: Apply Conditions

Refine the selection by applying conditions to include only cities that are either significantly large in area (area > 3000) or have a substantial population (population > 5000000).

SELECT city_name FROM World WHERE area > 3000 OR population > 5000000

Final Output:

+-----------+ | city_name | +-----------+ | Tokyo | | Delhi | | Shanghai | | New York | +-----------+

For your reference, here is the complete final query:

SELECT city_name FROM World WHERE area > 3000 OR population > 5000000
🤖 Don't fully get this? Learn it with Claude

Stuck on Major Cities? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Major Cities** (Databases) and want to truly understand it. Explain Major Cities from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Major Cities** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Major Cities** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Major Cities** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes