[Haker Rank]Symmetric Pairs

정보희·2022년 3월 25일
0

SQL 공부

목록 보기
8/8

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

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

2) all such symmetric pairs in ascending order by the value of X.
-> ORDER BY X

3)List the rows such that X1 ≤ Y1.
-> HAVING X<=Y

SELECT X AS X1 FROM Functions
INNER JOIN Y AS Y2 ON X1+1=Y2
WHERE X<=Y
ORDER BY X

union

SELECT X AS X2 FROM Functions
INNER JOIN Y AS Y1 ON X2-1=Y1
WHERE X<=Y
ORDER BY X

profile
데이터 다루는 마케터가 되는 것이 꿈 입니다!

0개의 댓글

관련 채용 정보