5.2. Permutations

Mike Whiting asked for code to generate all the possible permutations of a string. This was a popular problem in the British Computer Journal a few decades ago. They published several Algol programs with good discussions at that time. A good reference website is http://portal.acm.orglcitatioN.cfm?id=356692, but you can find others. But because they were written for a procedural programming language, many of the articles focus on the sequence of generation of permutations, rather than just getting the entire set.

5.2.1. Permutations via Recursion

Alex Kuznetsov replied with a recursive CTE solution that assumes an auxiliary Sequence table. Begin by setting the :input_str = ’ABCDE’ and running this query:

WITH RECURSIVE Subsets ...

Get Joe Celko's Thinking in Sets: Auxiliary, Temporal, and Virtual Tables in SQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.