We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
SELECT X, Y FROM (
(SELECT FN1.X AS X, FN1.Y AS Y
FROM FUNCTIONS FN1 JOIN FUNCTIONS FN2
ON FN1.X = FN2.Y AND FN1.Y = FN2.X
AND FN1.X <> FN1.Y
AND FN1.X <= FN1.Y
)
UNION
(SELECT FN3.X AS X, FN3.Y AS Y
FROM FUNCTIONS FN3
WHERE FN3.X = FN3.Y
GROUP BY FN3.X, FN3.Y
HAVING COUNT(*) > 1
)
) ALIAS
ORDER BY X
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Symmetric Pairs
You are viewing a single comment's thread. Return to all comments →
Without using CTEs:-
SELECT X, Y FROM ( (SELECT FN1.X AS X, FN1.Y AS Y
FROM FUNCTIONS FN1 JOIN FUNCTIONS FN2 ON FN1.X = FN2.Y AND FN1.Y = FN2.X
AND FN1.X <> FN1.Y AND FN1.X <= FN1.Y ) UNION (SELECT FN3.X AS X, FN3.Y AS Y FROM FUNCTIONS FN3 WHERE FN3.X = FN3.Y GROUP BY FN3.X, FN3.Y HAVING COUNT(*) > 1 ) ) ALIAS ORDER BY X