
PUZZLE 40 PERMUTATIONS 163
PUZZLE
40 PERMUTATIONS
SQL is pretty good about letting you do CROSS JOINs to get all possible
pairs (
x, y) from two sets of elements with a simple query, for example:
SELECT x, y
FROM BigX CROSS JOIN BigY;
But sometimes you would like to do this sort of thing horizontally
instead of vertically. A permutation is an ordered arrangement of
elements of a set. For example, if I have the set {1, 2, 3}, the
permutations of those elements are (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1),
(3, 1, 2), and (3, 2, 1). The rule is that for (n) elements, you have a
factorial number (n!) of permutations. What I would like is a query that ...