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.

Table `flights` has one row per flight: - `id` (INTEGER, primary key): flight id - `src` (VARCHAR(50), NOT NULL): city the flight departs from - `dest` (VARCHAR(50), NOT NULL): city the flight arrives in 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. Output columns, in this order: - `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. The pair (`src`, `dest`) is unique in the output, so this order is fully determined. There is nothing to round. 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. Example: if `flights` holds (1, Berlin, Amsterdam), (2, Amsterdam, Berlin), (3, Berlin, Amsterdam) and (4, Rome, Berlin), the result is (Amsterdam, Berlin) followed by (Berlin, Rome). The three flights between Berlin and Amsterdam are one route, and the Rome to Berlin route is written with Berlin first because Berlin sorts before Rome.

Tables

flights(id INTEGER, src VARCHAR(50), dest VARCHAR(50))

Hints

  1. Think about how to rewrite each row so that a flight and its reverse produce exactly the same pair of values.
  2. A route flown in only one direction still counts, whichever city it departs from, so do not rely on finding a matching return flight.

Community answers

Answer by ashokraminedi153

SELECT DISTINCT LEAST(src, dest) as src, GREATEST(src, dest) as dest from flights order by src, dest;
AppleData ScientisteasyData Manipulation (SQL/Python)

Distinct Flight Routes Between Cities

Table flights has one row per flight:

  • id (INTEGER, primary key): flight id
  • src (VARCHAR(50), NOT NULL): city the flight departs from
  • dest (VARCHAR(50), NOT NULL): city the flight arrives in

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.

Output columns, in this order:

  • 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. The pair (src, dest) is unique in the output, so this order is fully determined. There is nothing to round.

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.

Example: if flights holds (1, Berlin, Amsterdam), (2, Amsterdam, Berlin), (3, Berlin, Amsterdam) and (4, Rome, Berlin), the result is (Amsterdam, Berlin) followed by (Berlin, Rome). The three flights between Berlin and Amsterdam are one route, and the Rome to Berlin route is written with Berlin first because Berlin sorts before Rome.

Expected Output

Tables

flights
flights
ColumnType
idINTEGER
srcVARCHAR(50)
destVARCHAR(50)

Comments (1)

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions