Advance SQL Challenge: Permutation

365 Days of Daily Coding: Day 92

I went to watch an independent movie with my friend. It was about a teenager whose mother was suffering from pancreatic cancer and she wanted to find a way to have her mother near her even after she passed way. For this, she explored life after death beliefs of several religion.

I solved an advanced SQL challenge today and I would like to think I was able to solve it by mere chance while I was simply playing around for a solution.

Advance SQl Challenge

WITH temp AS (
Select 'A' As Val FROM Dual Union All
Select 'B' As Val FROM Dual Union All
Select 'C' As Val FROM Dual 
), sub AS (
Select Val, 1 AS l 
FROM temp)

SELECT ROW_NUMBER() OVER(ORDER BY Val1) AS "Row Number", Val1 || ','|| Val2 || ',' || Val3 AS Output
FROM(
SELECT c1.Val AS Val1, c2.Val AS Val2, c3.Val AS Val3
FROM sub c1 JOIN sub c2
ON c1.l = c2.l
JOIN sub c3
ON c1.l = c3.l
WHERE c1.Val <> c2.Val AND c1.Val <> c3.Val AND c2.Val <> c3.Val)

Leave a comment