문제

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.

 

*

Join문을 활용해서 구할 수 있는 문제이다.

 

 

실행 코드

 

1) X!=Y, 즉 X<Y인 케이스 -> INNER JOIN으로 짝 구하기

2) X=Y 인 케이스 -> HAVING COUNT(*) >= 2 조건을 추가해서 짝 구하기

 

두 가지 케이스를 UNION으로 합쳐주면 간단하게 풀 수 있다.

+ Recent posts