# Symmetric Pairs Solution

Hello Friends in this article i am gone to share Hacker Rank SQL Solutions with you | Symmetric Pairs Solution

Also Visit:  Placements Solution

#### Problem

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```

## Solution – Symmetric Pairs

##### MySQL Code
```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;

```

### What are symmetric pairs in Hackerrank?

Two pairs (a, b) and (c, d) are said to be symmetric if c is equal to b and a is equal to d. For example, (10, 20) and (20, 10) are symmetric. Given an array of pairs find all symmetric pairs in it

### How do you find a symmetric pair?

For every pair, check if its second element is found in the hash table. If yes, then compare the first element with the value of the matched entry of the hash table. If the value and the first element match, it is displayed as a symmetric pair.

Disclaimer: The above Problems are generated by Hacker Rank but the Solutions are Provided by NYANDER.COM. All Hacker Rank SQL Solutions Shared only for Educational and Learning Purpose.