Symmertric Pairs (HackerRank)

yonny·2022년 10월 10일
0
post-custom-banner

Advanced Join

문제

https://www.hackerrank.com/challenges/symmetric-pairs/problem?h_r=internal-search

You are given a table, Functions, containing two columns: X and Y.
Table

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
sample input

Sample Output
20 20
20 21
22 23

<내 코드>

SELECT x,y
FROM functions
WHERE x=y
GROUP BY x,y
HAVING count(*) = 2
UNION
SELECT f1.x, f1.y
FROM functions AS f1
    INNER JOIN functions AS f2
    ON f1.x = f2.y AND f1.y = f2.x
WHERE f1.x < f1.y
ORDER BY x;
post-custom-banner

0개의 댓글