How to make SQL query to calculate the range from permutation of 123 ex: select 123 from dual; --123!

Result

123
 132
 213
 231
 321
 312



6 rows return
Maybe this:

WITH Nums(Num) AS (
    SELECT 1 UNION SELECT 2 UNION SELECT 3
)
SELECT *
FROM Nums x
CROSS JOIN Nums y
CROSS JOIN Nums z
WHERE x.Num &lt;&gt; y.Num
AND x.Num &lt;&gt; z.Num
AND y.Num &lt;&gt; z.Num
ORDER BY x.Num, y.Num, z.Num


Assumes you are using mysql 8+ with CTEs. Also I have not tested this on mysql, only SQL Server, but hopefully the syntax is the same. Also note your 2nd to last and last results appear to be out of order, unsure if this was intentional.