Each row of flights is one flight from a source city to a destination city. A route is an unordered pair of cities: a flight from A to B and a flight from B to A belong to the same route, and repeated flights on the same route still make one route.
Write a query that returns every distinct route once, with the lexicographically smaller city as src and the larger city as dest, ordered by src ascending, then dest ascending.
Input Table(s)
flights
| Column | Type | Meaning |
|---|
| id | INT | Flight id |
| src | VARCHAR(50) | City the flight departs from, NOT NULL |
| dest | VARCHAR(50) | City the flight arrives in, NOT NULL |
Primary key: id.
Output Contract
| Column | Type | Meaning |
|---|
| src | VARCHAR(50) | The city of the route that sorts first |
| dest | VARCHAR(50) | The city of the route that sorts last |
Return one row per distinct route. Order by src ascending, then dest ascending. The pair (src, dest) is unique in the output, so this order is fully determined. There is nothing to round.
Example
flights
| id | src | dest |
|---|
| 1 | Berlin | Amsterdam |
| 2 | Amsterdam | Berlin |
| 3 | Berlin | Amsterdam |
| 4 | Rome | Berlin |
Expected result
| src | dest |
|---|
| Amsterdam | Berlin |
| Berlin | Rome |
There are three flights between Berlin and Amsterdam (two in one direction, one in the other), and they all represent the same route, Amsterdam-Berlin. Rome-Berlin is a separate route, written with Berlin first because Berlin sorts before Rome. The result has 2 distinct routes.
Constraints and Clarifications
-
At most 10,000 rows.
-
src
and
dest
are non-null strings of up to 50 characters and are never equal in the same row, so every route has two different cities.
-
The same direction can appear many times, and both directions can appear. Each route still appears exactly once in the output.
-
A route flown in only one direction is still a route, whichever city it departs from.
-
"Smaller" and "larger" mean ordinary string comparison, the same ordering
ORDER BY
uses on these columns. City names in the data are capitalized consistently.