Problem:-
Solution:-
select x, y from functions f1
where exists(select * from functions f2 where f2.y=f1.x
and f2.x=f1.y and f2.x>f1.x) and (x!=y)
union
select x, y from functions f1 where x=y and
((select count(*) from functions where x=f1.x and y=f1.x)>1)
order by x;
You are given a table, Functions, containing two columns: X and Y.
![](https://s3.amazonaws.com/hr-challenge-images/12892/1443818798-51909e977d-1.png)
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.
Sample Input
![](https://s3.amazonaws.com/hr-challenge-images/12892/1443818693-b384c24e35-2.png)
Sample Output
20 20
20 21
22 23
Solution:-
select x, y from functions f1
where exists(select * from functions f2 where f2.y=f1.x
and f2.x=f1.y and f2.x>f1.x) and (x!=y)
union
select x, y from functions f1 where x=y and
((select count(*) from functions where x=f1.x and y=f1.x)>1)
order by x;
No comments:
Post a Comment