You are given a table Coordinates containing pairs of integer coordinates.
Symmetric coordinates are defined as two coordinate pairs (X1, Y1) and (X2, Y2) where X1 == Y2 and X2 == Y1.
Write a SQL query to find all unique symmetric coordinate pairs that satisfy the condition X <= Y.
Requirements:
- Return only coordinates where
X <= Yto avoid duplicates - Order results by
Xascending, then byYascending - Handle duplicate coordinate pairs in the input table
Table Schema
| Column Name | Type | Description |
|---|---|---|
X
|
int | X coordinate value |
Y
|
int | Y coordinate value |
Input & Output
| X | Y |
|---|---|
| 1 | 2 |
| 2 | 1 |
| 3 | 3 |
| 4 | 5 |
| X | Y |
|---|---|
| 1 | 2 |
| 3 | 3 |
(1,2) and (2,1) are symmetric because X1=Y2=1 and Y1=X2=2. We return (1,2) since 1≤2.
(3,3) is symmetric with itself since X=Y=3.
(4,5) has no symmetric partner in the table, so it's excluded.
| X | Y |
|---|---|
| 1 | 1 |
| 1 | 1 |
| 2 | 3 |
| X | Y |
|---|---|
| 1 | 1 |
The duplicate (1,1) entries are symmetric with themselves. DISTINCT ensures we return only one (1,1) result.
(2,3) has no symmetric partner (3,2) in the table, so it's excluded from results.
| X | Y |
|---|---|
| 1 | 5 |
| 2 | 4 |
| 3 | 7 |
| X | Y |
|---|
None of the coordinates have symmetric partners in the table:
• (1,5) would need (5,1) - not present
• (2,4) would need (4,2) - not present
• (3,7) would need (7,3) - not present
Result is empty.
Constraints
-
1 ≤ coordinates count ≤ 1000 -
-1000 ≤ X, Y ≤ 1000 - Table may contain duplicate coordinate pairs