[해커랭크] Symmetric Pairs

june·2023년 4월 9일
0

SQL

목록 보기
18/31

Symmetric Pairs

https://www.hackerrank.com/challenges/symmetric-pairs

  • 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.
SELECT x, y
FROM functions f
WHERE x = y
GROUP BY x, y
HAVING COUNT(*) = 2
UNION -- 이어붙이기
SELECT f1.x, f1.y
FROM functions f1
    INNER JOIN functions f2 ON f1.x = f2.y AND f1.y = f2.x
WHERE f1.x < f1.y
ORDER BY x
profile
나의 계절은

0개의 댓글