-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3198-FindCitiesInEachState.sql
More file actions
67 lines (63 loc) · 2.8 KB
/
3198-FindCitiesInEachState.sql
File metadata and controls
67 lines (63 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-- 3198. Find Cities in Each State
-- Table: cities
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | state | varchar |
-- | city | varchar |
-- +-------------+---------+
-- (state, city) is the primary key (combination of columns with unique values) for this table.
-- Each row of this table contains the state name and the city name within that state.
-- Write a solution to find all the cities in each state and combine them into a single comma-separated string.
-- Return the result table ordered by state in ascending order.
-- The result format is in the following example.
-- Example:
-- Input:
-- cities table:
-- +-------------+---------------+
-- | state | city |
-- +-------------+---------------+
-- | California | Los Angeles |
-- | California | San Francisco |
-- | California | San Diego |
-- | Texas | Houston |
-- | Texas | Austin |
-- | Texas | Dallas |
-- | New York | New York City |
-- | New York | Buffalo |
-- | New York | Rochester |
-- +-------------+---------------+
-- Output:
-- +-------------+---------------------------------------+
-- | state | cities |
-- +-------------+---------------------------------------+
-- | California | Los Angeles, San Diego, San Francisco |
-- | New York | Buffalo, New York City, Rochester |
-- | Texas | Austin, Dallas, Houston |
-- +-------------+---------------------------------------+
-- Explanation:
-- California: All cities ("Los Angeles", "San Diego", "San Francisco") are listed in a comma-separated string.
-- New York: All cities ("Buffalo", "New York City", "Rochester") are listed in a comma-separated string.
-- Texas: All cities ("Austin", "Dallas", "Houston") are listed in a comma-separated string.
-- Note: The output table is ordered by the state name in ascending order.
-- Create table if not exists cities( state varchar(100),city varchar(100))
-- Truncate table cities
-- insert into cities (state, city) values ('California', 'Los Angeles')
-- insert into cities (state, city) values ('California', 'San Francisco')
-- insert into cities (state, city) values ('California', 'San Diego')
-- insert into cities (state, city) values ('Texas', 'Houston')
-- insert into cities (state, city) values ('Texas', 'Austin')
-- insert into cities (state, city) values ('Texas', 'Dallas')
-- insert into cities (state, city) values ('New York', 'New York City')
-- insert into cities (state, city) values ('New York', 'Buffalo')
-- insert into cities (state, city) values ('New York', 'Rochester')
-- Write your MySQL query statement below
SELECT
state,
GROUP_CONCAT(DISTINCT city ORDER BY city SEPARATOR ', ') AS cities
FROM
cities
GROUP BY
state
ORDER BY
state