Symmetric Pairs
https://www.hackerrank.com/challenges/symmetric-pairs
- You are given a table, Functions, containing two columns: X and Y. Two pairs (X1, Y1) and (X2, Y2) are said to be symmetric pairs if X1 = Y2 and X2 = Y1.
Write a query to output all such symmetric pairs in ascending order by the value of X. List the rows such that X1 ≤ Y1.
SELECT x, y
FROM functions f
WHERE x = y
GROUP BY x, y
HAVING COUNT(*) = 2
UNION
SELECT f1.x, f1.y
FROM functions f1
INNER JOIN functions f2 ON f1.x = f2.y AND f1.y = f2.x
WHERE f1.x < f1.y
ORDER BY x