cs

https://www.hackerrank.com/challenges/symmetric-pairs/problem?isFullScreen=true&h_r=next-challenge&h_v=zen 

 

Symmetric Pairs | HackerRank

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

www.hackerrank.com


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


[My Answer]

SELECT F1.x, F1.y
FROM FUNCTIONS AS F1
INNER JOIN 
FUNCTIONS AS F2 
ON F1.x=F2.y AND F1.y=F2.x
GROUP BY F1.x, F1.y
HAVING count(*)>=2 or F1.x < F1.y 
ORDER BY F1.x

Union으로 풀는 방식은 이해가 잘 되지 않는다,, 그냥 having으로 조건 주는게 더 편했다

'Code Problems > SQL_ HackerRank' 카테고리의 다른 글

[Hacker Rank] Top Competitors  (0) 2022.02.23
[Hacker Rank] The Report  (0) 2022.02.06
[Hacker Rank] Placement  (0) 2022.01.24
[HackerRank] Average Population of Each Continent  (0) 2022.01.04
[HackerRank] African Cities  (0) 2022.01.03

+ Recent posts