Symmetric Pairs

  • + 0 comments

    This is my solution. What I like about it is that all the conditions are in one line, so it is easy remove or add to conditions in one place. Also, the problem description isn't very clear that we should return only distict values, I had to assume that from the example and then from the multiple fail test (yes you're not alone if you run into this issue)

    WITH number_functions AS (
        SELECT 
            X, 
            Y, 
            ROW_NUMBER() OVER (ORDER BY X) AS rn
        FROM Functions
    )
    SELECT DISTINCT f1.X, f1.Y FROM number_functions f1
    CROSS JOIN number_functions f2
    WHERE f1.X = f2.Y AND f2.X = f1.Y AND f1.X <= f1.Y AND f1.rn != f2.rn
    ORDER BY f1.X ASC, f1.Y ASC;