Find distinct flight routes where A to B is the same route as B to A

Quick Overview

Write a SQL query that lists every distinct flight route once, treating a flight from A to B and a flight from B to A as the same route. It tests normalizing unordered pairs of values, removing duplicates with aggregation, and producing a deterministic sort order.

Find distinct flight routes where A to B is the same route as B to A

Company: Apple

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

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. ```hint Canonical form Think about how to rewrite each row so that a flight and its reverse produce exactly the same pair of values. ```

Overview: Write a SQL query that lists every distinct flight route once, treating a flight from A to B and a flight from B to A as the same route. It tests normalizing unordered pairs of values, removing duplicates with aggregation, and producing a deterministic sort order.

Apple logo
Apple
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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

ColumnTypeMeaning
idINTFlight id
srcVARCHAR(50)City the flight departs from, NOT NULL
destVARCHAR(50)City the flight arrives in, NOT NULL

Primary key: id.

Output Contract

ColumnTypeMeaning
srcVARCHAR(50)The city of the route that sorts first
destVARCHAR(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

idsrcdest
1BerlinAmsterdam
2AmsterdamBerlin
3BerlinAmsterdam
4RomeBerlin

Expected result

srcdest
AmsterdamBerlin
BerlinRome

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.