Query Exercise: Return Routes in the Right Order

Query Exercises
53 Comments

I’mma be honest with you, dear reader: this query exercise stems from a client problem that I couldn’t figure out how to solve in the time constraints that I had, and it drove me crazy. I wanted to put hours into this to figure it out, and I was sure there’d be a fun T-SQL way to do it.

I was onsite at a client helping them design a database, and part of the database involved storing routes for stations. For the purposes of this blog post, say we’ve got a station for each letter of the alphabet:

The Stations table content looks like this:

Stations table content

Most of the time, we want our route to start at the lowest StationPhysicalOrder (in this case, 1 for A) and then proceed through the stations by their StationPhysicalOrder, like this:

Station order

However, during emergencies, we may need to override the routing. To handle that, we have an override routing table:

The contents of that table:

Station Routing Overrides

Your exercise this week is to return a result set that:

  • Starts with the station with the lowest StationPhysicalOrder
  • For each row, if there’s a row in StationRoutingOverride to dictate the next step, jump to that station
  • Otherwise, go to the station with the next StationPhysicalOrder

In this case, your successful result set will look like this, sorted in this order:

The Dude Abides

As a side note, it took me a good solid hour to write this demo in a way that I could share it publicly, and end up with a station routing list that beautifully illustrated the problem at stake. You would think it would be easy to ask an LLM like ChatGPT for a word that’s:

  • An isogram (a word that doesn’t repeat any letters)
  • That has two adjacent letters in alphabetical order (like AB and DE)
  • Starts with a lower letter of the alphabet
  • Ends with a higher letter of the alphabet

I only asked for the first two requirements, and lots of LLMs got the answers terribly wrong! Here’s ChatGPT 4o trying its best, for example:

Oh, sweet ChatGPT

HAHAHA, poor thing. Bless its robotic little heart. It struggled when trying to solve this query exercise, too – I think it was on the right track, and it showed signs that it vaguely understood what it was doing, but the first few attempts didn’t get across the finish line. I do think this is a really fun challenge to use with LLMs, though – it’s the kind of thing where AI can write a query in a matter of seconds, but then it’ll take you minutes or hours to understand what the heck it’s doing, let alone troubleshoot the hidden problems.

Anyhoo, back to the challenge. I’m not being humble when I say I don’t think I’m very good at T-SQL, and this week’s Query Exercise is a great example. I stared at this client problem for a good 20 minutes, and I couldn’t come up with a set-based way to do it. I could think of procedural ways to do it, like loop through the stations one at a time with a while loop, cursor, or row-by-row function, but I couldn’t come up with a simple set-based way. I told the client, “I’m no Itzik Ben-Gan, but I bet if I post this as a Query Exercise on the blog, a bunch of readers will come up with awesome answers.” For the record, I’m not asking you to do my work: I came up with a quick way to return the data without the correct sorting, and the client’s app sorts it client-side.

Give it your best shot first, experimenting with ideas, because this is WAY harder than it looks. Then check out the solutions from other folks, compare and contrast your work, and finally read my answers and thoughts.

Previous Post
Who’s Hiring in the Microsoft Database Community? June 2025 Edition
Next Post
[Video] Office Hours in the Detroit Airport

53 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.