Advanced SQL: Using CrossJoin

365 Days of Daily Coding: Day 127

I woke up quite early today and I think I did not get enough sleep in my opinion so I am feeling a bit dull this hour. I hope I can shake it off after I have my breakfast. I ordered egg, cheese and ham croissant from grab. Looking forward to my breakfast as I am quite hungry tbh. Now, onto the puzzle.

Puzzle 11: Permutations

I tried the solution in the live oracle environment here.

With temp AS (
SELECT 'A' As TestCase FROM Dual UNION ALL
SELECT 'B' As TestCase FROM Dual UNION ALL
SELECT 'C' As TestCase FROM Dual
)
SELECT  ROWNUM AS RowNumber, TestCase1 || ',' || TestCase2 || ',' || TestCase3 AS Output
FROM(
SELECT t.TestCase AS TestCase1, a.TestCase AS TestCase2, k.TestCase AS TestCase3
FROM temp t CROSS JOIN temp a CROSS JOIN temp k
WHERE t.TestCase <> a.TestCase AND a.TestCase <> k.TestCase AND t.TestCase <> k.TestCase
ORDER BY TestCase1);

Leave a comment