Symmetric Pairs
JOIN
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.
Sample Input

Sample Output
20 20
20 21
22 23
/*
- GROUP BY f1.x, f1.y HAVING COUNT(f1.x) > 1 is used to remove duplicate records
- f1.x < f1.y is used to keep the rest in the result
*/
SELECT
f1.x as x1,
f1.y as y1
-- f2.x as x2,
-- f2.y as y2
FROM functions f1
JOIN functions f2
ON f1.x = f2.y and f2.x = f1.y
GROUP BY f1.x, f1.y HAVING COUNT(f1.x) > 1 OR f1.x < f1.y
ORDER BY f1.x
Last updated
Was this helpful?