OraQA

Oracle Question and Answer

  • Do you have a solution to a problem? Do you have an unanswered question? Login and share it with the Oracle community. More...

Oracle News


Entries RSS feed

Comments RSS feed

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

Leave a Reply

You must be logged in to post a comment.

RSS feed for comments on this question