How to solve the Ways To List 1, 2, … 10 Out of Order problem in SQL
July 6th, 2008 By Frank Zhou
The following is an interesting problem posted by mathforum.org:
How many ways are there to list the numbers one through ten so that
no number appears in its own position (i.e. 1 is not first in the
list, 2 is not second, three is not third, etc.)?
——————-SQL Solution ——————
SELECT count(*)
FROM
(SELECT LEVEL num FROM dual CONNECT BY LEVEL<=10)
WHERE LEVEL= 10
CONNECT BY NOCYCLE num != PRIOR num
AND num != LEVEL
AND CONNECT_BY_ROOT(num) != 1
AND LEVEL <=10;
COUNT(*)
———-
1334961
