Airport Name Lengths
Beginner Mode
Scenario
You are a data analyst at a major airline with three tables containing flight records, airport details, and plane specifications.
Task
Write a Snowflake SQL query that:
- Joins
{{ ref("flights") }}to{{ ref("airports") }}twice (once for the origin airport, once for the destination airport) using table aliases - Joins
{{ ref("flights") }}to{{ ref("planes") }}onflight_id = plane_id - Computes
origin_airport_name_lengthas the length of the trimmed origin airport name - Computes
destination_airport_name_lengthas the length of the trimmed destination airport name - Computes
plane_model_lengthas the length of the trimmed plane model name - Returns
flight_id,origin_airport_name_length,destination_airport_name_length, andplane_model_length
Schema
flights
| Column | Type | Description |
|---|---|---|
| flight_id | Integer | Unique flight identifier |
| origin_airport | String | Airport ID of the origin |
| destination_airport | String | Airport ID of the destination |
airports
| Column | Type | Description |
|---|---|---|
| airport_id | String | Unique airport identifier |
| airport_name | String | Full name of the airport (may have leading or trailing spaces) |
planes
| Column | Type | Description |
|---|---|---|
| plane_id | Integer | Unique plane identifier |
| plane_model | String | Model name of the plane (may have leading or trailing spaces) |
Example
flights:
| flight_id | origin_airport | destination_airport |
|---|---|---|
| 1 | JFK | LAX |
| 2 | ORD | MIA |
| 3 | SEA | DFW |
| 4 | ATL | SFO |
| 5 | BOS | DEN |
airports:
| airport_id | airport_name |
|---|---|
| JFK | John F Kennedy |
| LAX | Los Angeles |
| ORD | Chicago OHare |
| MIA | Miami |
| SEA | Seattle Tacoma |
| DFW | Dallas Fort Worth |
| ATL | Atlanta |
| SFO | San Francisco |
| BOS | Boston Logan |
| DEN | Denver |
planes:
| plane_id | plane_model |
|---|---|
| 1 | Boeing 787 |
| 2 | Airbus A321 |
| 3 | Boeing 737 |
| 4 | Embraer E190 |
| 5 | Airbus A350 |
Expected Output:
| flight_id | origin_airport_name_length | destination_airport_name_length | plane_model_length |
|---|---|---|---|
| 1 | 14 | 11 | 10 |
| 2 | 13 | 5 | 11 |
| 3 | 14 | 17 | 10 |
| 4 | 7 | 13 | 12 |
| 5 | 12 | 6 | 11 |
Note: Lengths are computed after trimming whitespace. For example, "John F Kennedy" has 14 characters, "Los Angeles" has 11, and "Boeing 787" has 10.
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Uber