Skip to main content

// advent-of-postgres/day-22

Day 22 of 24AdvancedRecursive CTEsShortest path

The Fastest Route

Plan the sleigh's route through the relay stations. The route with the fewest stops is not the fastest one.

Opens 22 December 2026, 00:00 UTC

Two days to go. The sleigh flies from the North Pole to Oslo through a chain of relay stations, and the route planner broke in the last deploy. Alabaster needs the fastest route by hand.

The relay hops are in hops. Each hop is one way:

Column Type Notes
from_stop text Where the hop starts
to_stop text Where it ends
minutes int Flying time for this hop

The sleigh starts at north-pole and can take any chain of hops. It never needs to visit a station twice.

Your task

What is the shortest total flying time from north-pole to oslo, in minutes?

Submit the number.

Stuck? Show a hint

Build every route with a recursive CTE that carries the total time and an array of visited stops (so it never loops), then take the minimum for oslo.

No database yet. You get a real Postgres database on Neon, free, with no account.
One statement at a time. Ctrl+Enter or Cmd+Enter runs it.

Download today's data as SQL to solve it on your own Postgres (14 or later).

Solved this puzzle? Share it.

Tag @thedevopsdaily and share it with the community.