[HackerRank MySQL] Symmetric Pairs
2021. 1. 11. 23:59ㆍToday I Learned.../MySQL
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.
Sample Input
Sample Output
20 20
20 21
22 23
Query
select f1.x, f1.y
from functions f1 inner join functions f2
on f1.x = f2.y and f2.x = f1.y
group by f1.x, f1.y
having count(*) >= 2 or f1.x < f1.y
order by 1
Reference: HackerRank Practice > SQL > Advanced Join > Symmetric Pairs
'Today I Learned... > MySQL' 카테고리의 다른 글
[LeetCode MySQL] 184. Department Highest Salary (0) | 2021.01.19 |
---|---|
[LeetCode MySQL] 180. Consecutive Numbers (0) | 2021.01.19 |
[LeetCode MySQL] 181. Employees Earning More Than Their Managers (0) | 2021.01.11 |
[LeetCode MySQL] 197. Rising Temperature (0) | 2021.01.11 |
[LeetCode MySQL] 183. Customers Who Never Order (0) | 2021.01.11 |