혜온의 이것저것

[HackerRank] Symmetric Paris (MySQL) 본문

Data Analysis/SQL

[HackerRank] Symmetric Paris (MySQL)

혜온 :) 2022. 9. 22. 16:30

https://www.hackerrank.com/challenges/symmetric-pairs/problem?isFullScreen=true 

 

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

Sample Output

20 20
20 21
22 23

 

풀이
select sub.x, sub.y
from (select if(x<=y,x,y) x, if(x<=y,y,x) y
	from functions) sub
group by sub.x, sub.y
having count(*)>=2
order by sub.x
Comments