Stars and Planets
Beginner Mode
Scenario
You are an astronomer at a space observatory with two tables containing star data and planet data.
Task
Write a Snowflake SQL query that:
- Joins
{{ ref("planets") }}with{{ ref("stars") }}using the planet'sstar_idcolumn and the star'sidcolumn - Aliases the star columns:
nameasstar_name,colorasstar_color,typeasstar_type - Aliases the planet columns:
nameasplanet_name,typeasplanet_type - Aliases
stars.distanceasdistance_star_earthandplanets.distanceasdistance_planet_star - Returns all 7 output columns
Schema
stars
| Column | Type | Description |
|---|---|---|
| id | Integer | Unique identifier for the star |
| name | String | Name of the star |
| color | String | Color of the star |
| type | String | Type of the star (Dwarf, Giant, Supergiant, etc.) |
| distance | Float | Distance from Earth in light years |
planets
| Column | Type | Description |
|---|---|---|
| id | Integer | Unique identifier for the planet |
| name | String | Name of the planet |
| star_id | Integer | ID of the star the planet orbits |
| type | String | Type of the planet (Gas Giant, Terrestrial, etc.) |
| distance | Float | Distance from its star in Astronomical Units (AU) |
Example
stars:
| id | name | color | type | distance |
|---|---|---|---|---|
| 1 | Alpha | Yellow | Dwarf | 4.3 |
| 2 | Beta | Blue | Giant | 12.0 |
| 3 | Gamma | Red | Supergiant | 550.0 |
planets:
| id | name | star_id | type | distance |
|---|---|---|---|---|
| 1 | Arion | 1 | Terrestrial | 0.8 |
| 2 | Brahe | 1 | Gas Giant | 5.2 |
| 3 | Calypso | 2 | Terrestrial | 1.1 |
| 4 | Draco | 3 | Gas Giant | 40.0 |
Expected Output:
| star_name | star_color | star_type | planet_name | planet_type | distance_star_earth | distance_planet_star |
|---|---|---|---|---|---|---|
| Alpha | Yellow | Dwarf | Arion | Terrestrial | 4.3 | 0.8 |
| Alpha | Yellow | Dwarf | Brahe | Gas Giant | 4.3 | 5.2 |
| Beta | Blue | Giant | Calypso | Terrestrial | 12.0 | 1.1 |
| Gamma | Red | Supergiant | Draco | Gas Giant | 550.0 | 40.0 |
Note: Both
starsandplanetssharename,type, anddistancecolumns, so the output renames each to avoid ambiguity (e.g.,star_namevsplanet_name).
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 →
Cloudflare