Recursive SQL (DB2): (WITH X defines a temporary view) WITH trips(destination, route, totalcost) AS ((SELECT destination, destination, cost FROM flights WHERE origin = "SFO") UNION ALL (SELECT f.destination, t.route || ',' || f.destination, t.totalcost * f.cost FROM trips t, flights f WHERE t.destination = f.origin)) SELECT route, totalcost FROM trips WHERE destination = "JFK"; NB: There is no stop condition, so this will run indefinitely..