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:

  1. Joins {{ ref("planets") }} with {{ ref("stars") }} using the planet's star_id column and the star's id column
  2. Aliases the star columns: name as star_name, color as star_color, type as star_type
  3. Aliases the planet columns: name as planet_name, type as planet_type
  4. Aliases stars.distance as distance_star_earth and planets.distance as distance_planet_star
  5. 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 stars and planets share name, type, and distance columns, so the output renames each to avoid ambiguity (e.g., star_name vs planet_name).

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →