Snoopy [해커랭크/MySQL] 45. Symmetric Pairs
코딩테스트/해커랭크 SQL

[해커랭크/MySQL] 45. Symmetric Pairs

Sooyoon Jeong 2023. 4. 14.

🤔 문제 분석

1. Two pairs (X1, Y1) and (X2, Y2) are said to be symmetric pairs if X1 = Y2 and X2 = Y1.

2. Write a query to output all such symmetric pairs in ascending order by the value of X.

3. List the rows such that X1 ≤ Y1.

 

💡 풀이

SELECT A.X, A.Y
FROM Functions AS A
    JOIN Functions AS B ON A.Y = B.X AND A.X = B.Y
GROUP BY A.X, A.Y
HAVING COUNT(*) > 1 OR A.X < A.Y
ORDER BY A.X

 

댓글