Find Cities in Each State II - Problem
Find Cities in Each State II
You're working as a data analyst for a travel company that needs to analyze city data across different states. Given a database table of cities and their corresponding states, your task is to create a comprehensive report with specific filtering criteria.
The Challenge:
• Group all cities by state into comma-separated lists
• Filter states that have at least 3 cities
• Only include states where at least one city starts with the same letter as the state name
• Sort results by the count of "matching-letter" cities (descending), then by state name (ascending)
Input: A
Output: A result table with
This problem tests your SQL skills in grouping, filtering, string manipulation, and complex sorting criteria.
You're working as a data analyst for a travel company that needs to analyze city data across different states. Given a database table of cities and their corresponding states, your task is to create a comprehensive report with specific filtering criteria.
The Challenge:
• Group all cities by state into comma-separated lists
• Filter states that have at least 3 cities
• Only include states where at least one city starts with the same letter as the state name
• Sort results by the count of "matching-letter" cities (descending), then by state name (ascending)
Input: A
cities table with columns state and cityOutput: A result table with
state, cities (comma-separated), and matching_letter_countThis problem tests your SQL skills in grouping, filtering, string manipulation, and complex sorting criteria.
Input & Output
example_1.sql — Basic Example
$
Input:
cities table:
+--------------+---------------+
| state | city |
+--------------+---------------+
| New York | New York City |
| New York | Newark |
| New York | Buffalo |
| New York | Rochester |
| California | San Francisco |
| California | Sacramento |
| California | San Diego |
| California | Los Angeles |
| Texas | Tyler |
| Texas | Temple |
| Texas | Taylor |
| Texas | Dallas |
| Pennsylvania | Philadelphia |
| Pennsylvania | Pittsburgh |
| Pennsylvania | Pottstown |
+--------------+---------------+
›
Output:
+-------------+-------------------------------------------+-----------------------+
| state | cities | matching_letter_count |
+-------------+-------------------------------------------+-----------------------+
| Pennsylvania| Philadelphia, Pittsburgh, Pottstown | 3 |
| Texas | Dallas, Taylor, Temple, Tyler | 3 |
| New York | Buffalo, Newark, New York City, Rochester | 2 |
+-------------+-------------------------------------------+-----------------------+
💡 Note:
Pennsylvania has all 3 cities starting with 'P' (matching state). Texas has 3 cities (Taylor, Temple, Tyler) starting with 'T'. New York has 2 cities (Newark, New York City) starting with 'N'. California is excluded because no cities start with 'C'.
example_2.sql — Edge Case - Minimum Requirements
$
Input:
cities table:
+----------+----------+
| state | city |
+----------+----------+
| Florida | Fort Myers|
| Florida | Fresno |
| Florida | Fairfield|
| Ohio | Orlando |
| Ohio | Omaha |
| Ohio | Oakland |
+----------+----------+
›
Output:
+--------+-------------------------+-----------------------+
| state | cities | matching_letter_count |
+--------+-------------------------+-----------------------+
| Florida| Fairfield, Fort Myers, Fresno | 3 |
+--------+-------------------------+-----------------------+
💡 Note:
Florida has exactly 3 cities and all start with 'F'. Ohio has 3 cities but none start with 'O', so it's excluded despite meeting the count requirement.
example_3.sql — Multiple States Same Count
$
Input:
cities table:
+--------+-----------+
| state | city |
+--------+-----------+
| Maine | Manchester|
| Maine | Miami |
| Maine | Portland |
| Nevada | Nashville |
| Nevada | Norfolk |
| Nevada | Newport |
| Nevada | Phoenix |
+--------+-----------+
›
Output:
+-------+---------------------------+-----------------------+
| state | cities | matching_letter_count |
+-------+---------------------------+-----------------------+
| Nevada| Nashville, Newport, Norfolk, Phoenix | 3 |
| Maine | Manchester, Miami, Portland | 2 |
+-------+---------------------------+-----------------------+
💡 Note:
When matching_letter_count is different (Nevada=3, Maine=2), Nevada comes first. Both states meet minimum requirements but Nevada has more matching cities.
Constraints
- 1 ≤ number of rows ≤ 1000
- 1 ≤ state.length, city.length ≤ 100
- state and city consist of English letters and spaces only
- Each (state, city) combination is unique
- At least one state will have ≥ 3 cities with ≥ 1 matching city
Visualization
Tap to expand
Understanding the Visualization
1
Scan Table
Database scans the cities table row by row
2
Group Formation
GROUP BY creates buckets for each unique state
3
Aggregation
For each group, compute city list and matching count
4
Filter Groups
HAVING clause eliminates groups not meeting criteria
5
Sort Results
ORDER BY sorts remaining groups by count and name
Key Takeaway
🎯 Key Insight: The database engine optimizes this complex query by processing groups efficiently, using indexes where available, and performing filtering and sorting in the most efficient order possible.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code