Reputation: 8731
Recently I came across with the following quiz. Imagine we have this table
+--------+
| colors |
+--------+
| red |
| black |
| white |
| green |
| orange |
+--------+
The task is to write a SQL query that will select all pairs without allowing duplicates. Permutations are counted too ({red, black} = {black, red}, hence only one of the pair is allowed).
Upvotes: 7
Views: 2429
Reputation: 3483
try this :
Select * From colours A
Cross Join colours B Where A.colours < B.colours or B.colours > A.colours
and b.colours != b.colours and a.colours != a.colours
Upvotes: 0
Reputation: 1
Select A.Colors, B.Colors From Colors A
Cross Join Colors B Where A.Colors < B.Colors or B.colors < A.colors
Upvotes: 0
Reputation: 146541
Try this
Select A.Color, B.Color
From Colors A
Cross Join Colors B
Where A.Color > B.Color
Upvotes: 27