Itzik Ben-Gan posted a challenge to his blog and to the SQL MVP newsgroup. The challenge was to be able to use T-SQL to create palindromes from a list of words. The words would have to be unique, so that the list wasn’t endless.
A handful of MVPs (including myself) pushed out potential solutions. All slightly different of course, because of the creativity possible within T-SQL. Itzik posted the results at http://www.sqlmag.com/Article/ArticleID/95008/sql_server_95008.html and it turns out my query was the fastest. Go figure! I’m not sure that Adam (Machanic) or Steve (Kass) tried to optimise theirs much. I imagine if we all tried to find ways to improve our queries, they could all become a bit faster again.
The key thing with something like palindromes is to build them up as you go, looking for candidate words that can continue the sequences from the start and end – and this makes them a great candidate for CTEs. I also wonder if you could easily build them up from the middle, but that would then mean keeping a track of where the middle was… and I’m not sure it would end up as fast. Maybe another day.