glaz666
glaz666

Reputation: 8731

Interesting SQL quiz

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

Answers (3)

User Learning
User Learning

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

inbaselvan
inbaselvan

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

Charles Bretana
Charles Bretana

Reputation: 146541

Try this

Select A.Color, B.Color
From Colors A
Cross Join Colors B
Where A.Color > B.Color

Upvotes: 27

Related Questions