The very basics
Infinite looping
Processing trees with recursive CTEs
Processing graphs with recursive ctes
28. Travelling salesman 1


Okay! Quite an impressive query, wasn't it? Let's build it, step by step, from scratch now.


We start in London, so let's show the information for London first. We need the following columns:

  • Path – for now, it's simply the city name,
  • Lat,
  • Lon,
  • TotalDistance – the total distance travelled between cities – for now it's equal to 0.0,
  • CountPlaces – the number of cities visited; it's equal to 1 at this point.

All the information can be found in the City table.