Question: create table pairs( number a,number b)

where the data is 

```
1,1
1,2
1,3
2,1
2,2
2,3
3,1
3,2
3,3

```

etc.

what query gives me the unique pairs? 
With this i mean:

```
1,2 or 2,1
1,3    3,1
2,3    3,2

```

Answer: 
You may use a least/greatest trick here:

```
SELECT
    LEAST(a, b), GREATEST(a, b)
FROM yourTable
WHERE a <> b
GROUP BY
    LEAST(a, b), GREATEST(a, b);

```

To see why this works, consider the two pairs `(1, 2)` and `(2, 1)`. In both cases, the least resolves to `1` and the greatest to `2`, so both pairs would be reported as the same thing according to the logic in my query.

We could also tighten this up to the following:

```
SELECT DISTINCT LEAST(a, b), GREATEST(a, b)
FROM yourTable
WHERE a <> b;

```

Note: If you want to return pairs where `a = b`, then remove the `WHERE` clause.
```
SELECT sub.output FROM
(
SELECT  CONCAT(a,b, ' or ',
b,a ) output,
ASCII(a)+ASCII(b) asci
FROM tbl_t1 WHERE a!=b
) sub GROUP BY sub.asci;

```