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 cities table with columns state and city
Output: A result table with state, cities (comma-separated), and matching_letter_count

This 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
SQL Query Execution VisualizationOriginal Cities Tablestate | city pairs from input dataTexas GroupTylerTempleTaylor, DallasNew York GroupNewarkBuffaloNYC, RochesterPennsylvaniaPhiladelphiaPittsburghPottstownCaliforniaLos AngelesSan FranciscoSacramento, SDAggregation PhaseTexasCount: 4 ✓Matching: 3 ✓New YorkCount: 4 ✓Matching: 2 ✓PennsylvaniaCount: 3 ✓Matching: 3 ✓CaliforniaCount: 4 ✓Matching: 0 ✗Final Sorted Results1. Pennsylvania (3 matches)Philadelphia, Pittsburgh, Pottstown2. Texas (3 matches)Dallas, Taylor, Temple, Tyler3. New York (2 matches)Buffalo, Newark, NYC, Rochester❌ California(Filtered out)ORDER BY: matching_letter_count DESC, state ASC
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.
Asked in
Amazon 45 Google 35 Microsoft 28 Meta 22 Apple 18
67.0K Views
Medium-High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen