split out navigation sql code into a nav_query function