# Solving Sudoku with SQL

OK, first let me admit that this is in no way something that I would recommend doing. Also, let me admit straight out that the method implemented here is a complete BFI approach, and is not the optimal solution to the sudoku problem.

Having laid down what this is not, why do this anyway? I did this just as a test to see how a number of databases would handle something that is possibly more complex than the usual SQL statement that is used for testing.

## How is it done?

To solve the problem, I created a small table:

CREATE TABLE s (x INT);

and inserted 9 rows into it:

INSERT INTO s (x) VALUES (1);

INSERT INTO s (x) VALUES (2);

INSERT INTO s (x) VALUES (3);

INSERT INTO s (x) VALUES (4);

INSERT INTO s (x) VALUES (5);

INSERT INTO s (x) VALUES (6);

INSERT INTO s (x) VALUES (7);

INSERT INTO s (x) VALUES (8);

INSERT INTO s (x) VALUES (9);

Now, s(x) gives the possible values that can be in each of the 81 squares on the sudoku board.

The puzzle can be solved by assuming that we have a copy of this table representing each of the 81 cells on the sudoku board, and we need to select the appropriate value for each cell based on the usual rules of sudoku, and the given clues at the start of the game.

This results in a 81-way self-join of this table, with the join condition being based on the inequality that each cell must be different from other cells in the same row, column and 3x3 square.

SELECT a1.x as a1,

a2.x as a2,

a3.x as a3,

a4.x as a4,

a5.x as a5,

a6.x as a6,

a7.x as a7,

a8.x as a8,

a9.x as a9,

b1.x as b1,

b2.x as b2,

b3.x as b3,

b4.x as b4,

b5.x as b5,

b6.x as b6,

b7.x as b7,

b8.x as b8,

b9.x as b9,

c1.x as c1,

c2.x as c2,

c3.x as c3,

c4.x as c4,

c5.x as c5,

c6.x as c6,

c7.x as c7,

c8.x as c8,

c9.x as c9,

d1.x as d1,

d2.x as d2,

d3.x as d3,

d4.x as d4,

d5.x as d5,

d6.x as d6,

d7.x as d7,

d8.x as d8,

d9.x as d9,

e1.x as e1,

e2.x as e2,

e3.x as e3,

e4.x as e4,

e5.x as e5,

e6.x as e6,

e7.x as e7,

e8.x as e8,

e9.x as e9,

f1.x as f1,

f2.x as f2,

f3.x as f3,

f4.x as f4,

f5.x as f5,

f6.x as f6,

f7.x as f7,

f8.x as f8,

f9.x as f9,

g1.x as g1,

g2.x as g2,

g3.x as g3,

g4.x as g4,

g5.x as g5,

g6.x as g6,

g7.x as g7,

g8.x as g8,

g9.x as g9,

h1.x as h1,

h2.x as h2,

h3.x as h3,

h4.x as h4,

h5.x as h5,

h6.x as h6,

h7.x as h7,

h8.x as h8,

h9.x as h9,

i1.x as i1,

i2.x as i2,

i3.x as i3,

i4.x as i4,

i5.x as i5,

i6.x as i6,

i7.x as i7,

i8.x as i8,

i9.x as i9

FROM s a1

JOIN s a2 ON a2.x <> a1.x

JOIN s a3 ON a3.x <> a1.x AND a3.x <> a2.x

JOIN s a4 ON a4.x <> a1.x AND a4.x <> a2.x AND a4.x <> a3.x

JOIN s a5 ON a5.x <> a1.x AND a5.x <> a2.x AND a5.x <> a3.x AND a5.x <> a4.x

JOIN s a6 ON a6.x <> a1.x AND a6.x <> a2.x AND a6.x <> a3.x AND a6.x <> a4.x AND a6.x <> a5.x

JOIN s a7 ON a7.x <> a1.x AND a7.x <> a2.x AND a7.x <> a3.x AND a7.x <> a4.x AND a7.x <> a5.x AND a7.x <> a6.x

JOIN s a8 ON a8.x <> a1.x AND a8.x <> a2.x AND a8.x <> a3.x AND a8.x <> a4.x AND a8.x <> a5.x AND a8.x <> a6.x AND a8.x <> a7.x

JOIN s a9 ON a9.x <> a1.x AND a9.x <> a2.x AND a9.x <> a3.x AND a9.x <> a4.x AND a9.x <> a5.x AND a9.x <> a6.x AND a9.x <> a7.x AND a9.x <> a8.x

JOIN s b1 ON b1.x <> a1.x AND b1.x <> a2.x AND b1.x <> a3.x

JOIN s b2 ON b2.x <> a1.x AND b2.x <> a2.x AND b2.x <> a3.x AND b2.x <> b1.x

JOIN s b3 ON b3.x <> a1.x AND b3.x <> a2.x AND b3.x <> a3.x AND b3.x <> b1.x AND b3.x <> b2.x

JOIN s b4 ON b4.x <> a4.x AND b4.x <> a5.x AND b4.x <> a6.x AND b4.x <> b1.x AND b4.x <> b2.x AND b4.x <> b3.x

JOIN s b5 ON b5.x <> a4.x AND b5.x <> a5.x AND b5.x <> a6.x AND b5.x <> b1.x AND b5.x <> b2.x AND b5.x <> b3.x AND b5.x <> b4.x

JOIN s b6 ON b6.x <> a4.x AND b6.x <> a5.x AND b6.x <> a6.x AND b6.x <> b1.x AND b6.x <> b2.x AND b6.x <> b3.x AND b6.x <> b4.x AND b6.x <> b5.x

JOIN s b7 ON b7.x <> a7.x AND b7.x <> a8.x AND b7.x <> a9.x AND b7.x <> b1.x AND b7.x <> b2.x AND b7.x <> b3.x AND b7.x <> b4.x AND b7.x <> b5.x AND b7.x <> b6.x

JOIN s b8 ON b8.x <> a7.x AND b8.x <> a8.x AND b8.x <> a9.x AND b8.x <> b1.x AND b8.x <> b2.x AND b8.x <> b3.x AND b8.x <> b4.x AND b8.x <> b5.x AND b8.x <> b6.x AND b8.x <> b7.x

JOIN s b9 ON b9.x <> a7.x AND b9.x <> a8.x AND b9.x <> a9.x AND b9.x <> b1.x AND b9.x <> b2.x AND b9.x <> b3.x AND b9.x <> b4.x AND b9.x <> b5.x AND b9.x <> b6.x AND b9.x <> b7.x AND b9.x <> b8.x

JOIN s c1 ON c1.x <> a1.x AND c1.x <> a2.x AND c1.x <> a3.x AND c1.x <> b1.x AND c1.x <> b2.x AND c1.x <> b3.x

JOIN s c2 ON c2.x <> a1.x AND c2.x <> a2.x AND c2.x <> a3.x AND c2.x <> b1.x AND c2.x <> b2.x AND c2.x <> b3.x AND c2.x <> c1.x

JOIN s c3 ON c3.x <> a1.x AND c3.x <> a2.x AND c3.x <> a3.x AND c3.x <> b1.x AND c3.x <> b2.x AND c3.x <> b3.x AND c3.x <> c1.x AND c3.x <> c2.x

JOIN s c4 ON c4.x <> a4.x AND c4.x <> a5.x AND c4.x <> a6.x AND c4.x <> b4.x AND c4.x <> b5.x AND c4.x <> b6.x AND c4.x <> c1.x AND c4.x <> c2.x AND c4.x <> c3.x

JOIN s c5 ON c5.x <> a4.x AND c5.x <> a5.x AND c5.x <> a6.x AND c5.x <> b4.x AND c5.x <> b5.x AND c5.x <> b6.x AND c5.x <> c1.x AND c5.x <> c2.x AND c5.x <> c3.x AND c5.x <> c4.x

JOIN s c6 ON c6.x <> a4.x AND c6.x <> a5.x AND c6.x <> a6.x AND c6.x <> b4.x AND c6.x <> b5.x AND c6.x <> b6.x AND c6.x <> c1.x AND c6.x <> c2.x AND c6.x <> c3.x AND c6.x <> c4.x AND c6.x <> c5.x

JOIN s c7 ON c7.x <> a7.x AND c7.x <> a8.x AND c7.x <> a9.x AND c7.x <> b7.x AND c7.x <> b8.x AND c7.x <> b9.x AND c7.x <> c1.x AND c7.x <> c2.x AND c7.x <> c3.x AND c7.x <> c4.x AND c7.x <> c5.x AND c7.x <> c6.x

JOIN s c8 ON c8.x <> a7.x AND c8.x <> a8.x AND c8.x <> a9.x AND c8.x <> b7.x AND c8.x <> b8.x AND c8.x <> b9.x AND c8.x <> c1.x AND c8.x <> c2.x AND c8.x <> c3.x AND c8.x <> c4.x AND c8.x <> c5.x AND c8.x <> c6.x AND c8.x <> c7.x

JOIN s c9 ON c9.x <> a7.x AND c9.x <> a8.x AND c9.x <> a9.x AND c9.x <> b7.x AND c9.x <> b8.x AND c9.x <> b9.x AND c9.x <> c1.x AND c9.x <> c2.x AND c9.x <> c3.x AND c9.x <> c4.x AND c9.x <> c5.x AND c9.x <> c6.x AND c9.x <> c7.x AND c9.x <> c8.x

JOIN s d1 ON d1.x <> a1.x AND d1.x <> b1.x AND d1.x <> c1.x

JOIN s d2 ON d2.x <> a2.x AND d2.x <> b2.x AND d2.x <> c2.x AND d2.x <> d1.x

JOIN s d3 ON d3.x <> a3.x AND d3.x <> b3.x AND d3.x <> c3.x AND d3.x <> d1.x AND d3.x <> d2.x

JOIN s d4 ON d4.x <> a4.x AND d4.x <> b4.x AND d4.x <> c4.x AND d4.x <> d1.x AND d4.x <> d2.x AND d4.x <> d3.x

JOIN s d5 ON d5.x <> a5.x AND d5.x <> b5.x AND d5.x <> c5.x AND d5.x <> d1.x AND d5.x <> d2.x AND d5.x <> d3.x AND d5.x <> d4.x

JOIN s d6 ON d6.x <> a6.x AND d6.x <> b6.x AND d6.x <> c6.x AND d6.x <> d1.x AND d6.x <> d2.x AND d6.x <> d3.x AND d6.x <> d4.x AND d6.x <> d5.x

JOIN s d7 ON d7.x <> a7.x AND d7.x <> b7.x AND d7.x <> c7.x AND d7.x <> d1.x AND d7.x <> d2.x AND d7.x <> d3.x AND d7.x <> d4.x AND d7.x <> d5.x AND d7.x <> d6.x

JOIN s d8 ON d8.x <> a8.x AND d8.x <> b8.x AND d8.x <> c8.x AND d8.x <> d1.x AND d8.x <> d2.x AND d8.x <> d3.x AND d8.x <> d4.x AND d8.x <> d5.x AND d8.x <> d6.x AND d8.x <> d7.x

JOIN s d9 ON d9.x <> a9.x AND d9.x <> b9.x AND d9.x <> c9.x AND d9.x <> d1.x AND d9.x <> d2.x AND d9.x <> d3.x AND d9.x <> d4.x AND d9.x <> d5.x AND d9.x <> d6.x AND d9.x <> d7.x AND d9.x <> d8.x

JOIN s e1 ON e1.x <> a1.x AND e1.x <> b1.x AND e1.x <> c1.x AND e1.x <> d1.x AND e1.x <> d2.x AND e1.x <> d3.x

JOIN s e2 ON e2.x <> a2.x AND e2.x <> b2.x AND e2.x <> c2.x AND e2.x <> d1.x AND e2.x <> d2.x AND e2.x <> d3.x AND e2.x <> e1.x

JOIN s e3 ON e3.x <> a3.x AND e3.x <> b3.x AND e3.x <> c3.x AND e3.x <> d1.x AND e3.x <> d2.x AND e3.x <> d3.x AND e3.x <> e1.x AND e3.x <> e2.x

JOIN s e4 ON e4.x <> a4.x AND e4.x <> b4.x AND e4.x <> c4.x AND e4.x <> d4.x AND e4.x <> d5.x AND e4.x <> d6.x AND e4.x <> e1.x AND e4.x <> e2.x AND e4.x <> e3.x

JOIN s e5 ON e5.x <> a5.x AND e5.x <> b5.x AND e5.x <> c5.x AND e5.x <> d4.x AND e5.x <> d5.x AND e5.x <> d6.x AND e5.x <> e1.x AND e5.x <> e2.x AND e5.x <> e3.x AND e5.x <> e4.x

JOIN s e6 ON e6.x <> a6.x AND e6.x <> b6.x AND e6.x <> c6.x AND e6.x <> d4.x AND e6.x <> d5.x AND e6.x <> d6.x AND e6.x <> e1.x AND e6.x <> e2.x AND e6.x <> e3.x AND e6.x <> e4.x AND e6.x <> e5.x

JOIN s e7 ON e7.x <> a7.x AND e7.x <> b7.x AND e7.x <> c7.x AND e7.x <> d7.x AND e7.x <> d8.x AND e7.x <> d9.x AND e7.x <> e1.x AND e7.x <> e2.x AND e7.x <> e3.x AND e7.x <> e4.x AND e7.x <> e5.x AND e7.x <> e6.x

JOIN s e8 ON e8.x <> a8.x AND e8.x <> b8.x AND e8.x <> c8.x AND e8.x <> d7.x AND e8.x <> d8.x AND e8.x <> d9.x AND e8.x <> e1.x AND e8.x <> e2.x AND e8.x <> e3.x AND e8.x <> e4.x AND e8.x <> e5.x AND e8.x <> e6.x AND e8.x <> e7.x

JOIN s e9 ON e9.x <> a9.x AND e9.x <> b9.x AND e9.x <> c9.x AND e9.x <> d7.x AND e9.x <> d8.x AND e9.x <> d9.x AND e9.x <> e1.x AND e9.x <> e2.x AND e9.x <> e3.x AND e9.x <> e4.x AND e9.x <> e5.x AND e9.x <> e6.x AND e9.x <> e7.x AND e9.x <> e8.x

JOIN s f1 ON f1.x <> a1.x AND f1.x <> b1.x AND f1.x <> c1.x AND f1.x <> d1.x AND f1.x <> d2.x AND f1.x <> d3.x AND f1.x <> e1.x AND f1.x <> e2.x AND f1.x <> e3.x

JOIN s f2 ON f2.x <> a2.x AND f2.x <> b2.x AND f2.x <> c2.x AND f2.x <> d1.x AND f2.x <> d2.x AND f2.x <> d3.x AND f2.x <> e1.x AND f2.x <> e2.x AND f2.x <> e3.x AND f2.x <> f1.x

JOIN s f3 ON f3.x <> a3.x AND f3.x <> b3.x AND f3.x <> c3.x AND f3.x <> d1.x AND f3.x <> d2.x AND f3.x <> d3.x AND f3.x <> e1.x AND f3.x <> e2.x AND f3.x <> e3.x AND f3.x <> f1.x AND f3.x <> f2.x

JOIN s f4 ON f4.x <> a4.x AND f4.x <> b4.x AND f4.x <> c4.x AND f4.x <> d4.x AND f4.x <> d5.x AND f4.x <> d6.x AND f4.x <> e4.x AND f4.x <> e5.x AND f4.x <> e6.x AND f4.x <> f1.x AND f4.x <> f2.x AND f4.x <> f3.x

JOIN s f5 ON f5.x <> a5.x AND f5.x <> b5.x AND f5.x <> c5.x AND f5.x <> d4.x AND f5.x <> d5.x AND f5.x <> d6.x AND f5.x <> e4.x AND f5.x <> e5.x AND f5.x <> e6.x AND f5.x <> f1.x AND f5.x <> f2.x AND f5.x <> f3.x AND f5.x <> f4.x

JOIN s f6 ON f6.x <> a6.x AND f6.x <> b6.x AND f6.x <> c6.x AND f6.x <> d4.x AND f6.x <> d5.x AND f6.x <> d6.x AND f6.x <> e4.x AND f6.x <> e5.x AND f6.x <> e6.x AND f6.x <> f1.x AND f6.x <> f2.x AND f6.x <> f3.x AND f6.x <> f4.x AND f6.x <> f5.x

JOIN s f7 ON f7.x <> a7.x AND f7.x <> b7.x AND f7.x <> c7.x AND f7.x <> d7.x AND f7.x <> d8.x AND f7.x <> d9.x AND f7.x <> e7.x AND f7.x <> e8.x AND f7.x <> e9.x AND f7.x <> f1.x AND f7.x <> f2.x AND f7.x <> f3.x AND f7.x <> f4.x AND f7.x <> f5.x AND f7.x <> f6.x

JOIN s f8 ON f8.x <> a8.x AND f8.x <> b8.x AND f8.x <> c8.x AND f8.x <> d7.x AND f8.x <> d8.x AND f8.x <> d9.x AND f8.x <> e7.x AND f8.x <> e8.x AND f8.x <> e9.x AND f8.x <> f1.x AND f8.x <> f2.x AND f8.x <> f3.x AND f8.x <> f4.x AND f8.x <> f5.x AND f8.x <> f6.x AND f8.x <> f7.x

JOIN s f9 ON f9.x <> a9.x AND f9.x <> b9.x AND f9.x <> c9.x AND f9.x <> d7.x AND f9.x <> d8.x AND f9.x <> d9.x AND f9.x <> e7.x AND f9.x <> e8.x AND f9.x <> e9.x AND f9.x <> f1.x AND f9.x <> f2.x AND f9.x <> f3.x AND f9.x <> f4.x AND f9.x <> f5.x AND f9.x <> f6.x AND f9.x <> f7.x AND f9.x <> f8.x

JOIN s g1 ON g1.x <> a1.x AND g1.x <> b1.x AND g1.x <> c1.x AND g1.x <> d1.x AND g1.x <> e1.x AND g1.x <> f1.x

JOIN s g2 ON g2.x <> a2.x AND g2.x <> b2.x AND g2.x <> c2.x AND g2.x <> d2.x AND g2.x <> e2.x AND g2.x <> f2.x AND g2.x <> g1.x

JOIN s g3 ON g3.x <> a3.x AND g3.x <> b3.x AND g3.x <> c3.x AND g3.x <> d2.x AND g3.x <> e3.x AND g3.x <> f3.x AND g3.x <> g1.x AND g3.x <> g2.x

JOIN s g4 ON g4.x <> a4.x AND g4.x <> b4.x AND g4.x <> c4.x AND g4.x <> d4.x AND g4.x <> e4.x AND g4.x <> f4.x AND g4.x <> g1.x AND g4.x <> g2.x AND g4.x <> g3.x

JOIN s g5 ON g5.x <> a5.x AND g5.x <> b5.x AND g5.x <> c5.x AND g5.x <> d5.x AND g5.x <> e5.x AND g5.x <> f5.x AND g5.x <> g1.x AND g5.x <> g2.x AND g5.x <> g3.x AND g5.x <> g4.x

JOIN s g6 ON g6.x <> a6.x AND g6.x <> b6.x AND g6.x <> c6.x AND g6.x <> d6.x AND g6.x <> e6.x AND g6.x <> f6.x AND g6.x <> g1.x AND g6.x <> g2.x AND g6.x <> g3.x AND g6.x <> g4.x AND g6.x <> g5.x

JOIN s g7 ON g7.x <> a7.x AND g7.x <> b7.x AND g7.x <> c7.x AND g7.x <> d7.x AND g7.x <> e7.x AND g7.x <> f7.x AND g7.x <> g1.x AND g7.x <> g2.x AND g7.x <> g3.x AND g7.x <> g4.x AND g7.x <> g5.x AND g7.x <> g6.x

JOIN s g8 ON g8.x <> a8.x AND g8.x <> b8.x AND g8.x <> c8.x AND g8.x <> d8.x AND g8.x <> e8.x AND g8.x <> f8.x AND g8.x <> g1.x AND g8.x <> g2.x AND g8.x <> g3.x AND g8.x <> g4.x AND g8.x <> g5.x AND g8.x <> g6.x AND g8.x <> g7.x

JOIN s g9 ON g9.x <> a9.x AND g9.x <> b9.x AND g9.x <> c9.x AND g9.x <> d9.x AND g9.x <> e9.x AND g9.x <> f9.x AND g9.x <> g1.x AND g9.x <> g2.x AND g9.x <> g3.x AND g9.x <> g4.x AND g9.x <> g5.x AND g9.x <> g6.x AND g9.x <> g7.x AND g9.x <> g8.x

JOIN s h1 ON h1.x <> a1.x AND h1.x <> b1.x AND h1.x <> c1.x AND h1.x <> d1.x AND h1.x <> e1.x AND h1.x <> f1.x AND h1.x <> g1.x AND h1.x <> g2.x AND h1.x <> g3.x

JOIN s h2 ON h2.x <> a2.x AND h2.x <> b2.x AND h2.x <> c2.x AND h2.x <> d2.x AND h2.x <> e2.x AND h2.x <> f2.x AND h2.x <> g1.x AND h2.x <> g2.x AND h2.x <> g3.x AND h2.x <> h1.x

JOIN s h3 ON h3.x <> a3.x AND h3.x <> b3.x AND h3.x <> c3.x AND h3.x <> d3.x AND h3.x <> e3.x AND h3.x <> f3.x AND h3.x <> g1.x AND h3.x <> g2.x AND h3.x <> g3.x AND h3.x <> h1.x AND h3.x <> h2.x

JOIN s h4 ON h4.x <> a4.x AND h4.x <> b4.x AND h4.x <> c4.x AND h4.x <> d4.x AND h4.x <> e4.x AND h4.x <> f4.x AND h4.x <> g4.x AND h4.x <> g5.x AND h4.x <> g6.x AND h4.x <> h1.x AND h4.x <> h2.x AND h4.x <> h3.x

JOIN s h5 ON h5.x <> a5.x AND h5.x <> b5.x AND h5.x <> c5.x AND h5.x <> d5.x AND h5.x <> e5.x AND h5.x <> f5.x AND h5.x <> g4.x AND h5.x <> g5.x AND h5.x <> g6.x AND h5.x <> h1.x AND h5.x <> h2.x AND h5.x <> h3.x AND h5.x <> h4.x

JOIN s h6 ON h6.x <> a6.x AND h6.x <> b6.x AND h6.x <> c6.x AND h6.x <> d6.x AND h6.x <> e6.x AND h6.x <> f6.x AND h6.x <> g4.x AND h6.x <> g5.x AND h6.x <> g6.x AND h6.x <> h1.x AND h6.x <> h2.x AND h6.x <> h3.x AND h6.x <> h4.x AND h6.x <> h5.x

JOIN s h7 ON h7.x <> a7.x AND h7.x <> b7.x AND h7.x <> c7.x AND h7.x <> d7.x AND h7.x <> e7.x AND h7.x <> f7.x AND h7.x <> g7.x AND h7.x <> g8.x AND h7.x <> g9.x AND h7.x <> h1.x AND h7.x <> h2.x AND h7.x <> h3.x AND h7.x <> h4.x AND h7.x <> h5.x AND h7.x <> h6.x

JOIN s h8 ON h8.x <> a8.x AND h8.x <> b8.x AND h8.x <> c8.x AND h8.x <> d8.x AND h8.x <> e8.x AND h8.x <> f8.x AND h8.x <> g7.x AND h8.x <> g8.x AND h8.x <> g9.x AND h8.x <> h1.x AND h8.x <> h2.x AND h8.x <> h3.x AND h8.x <> h4.x AND h8.x <> h5.x AND h8.x <> h6.x AND h8.x <> h7.x

JOIN s h9 ON h9.x <> a9.x AND h9.x <> b9.x AND h9.x <> c9.x AND h9.x <> d9.x AND h9.x <> e9.x AND h9.x <> f9.x AND h9.x <> g7.x AND h9.x <> g8.x AND h9.x <> g9.x AND h9.x <> h1.x AND h9.x <> h2.x AND h9.x <> h3.x AND h9.x <> h4.x AND h9.x <> h5.x AND h9.x <> h6.x AND h9.x <> h7.x AND h9.x <> h8.x

JOIN s i1 ON i1.x <> a1.x AND i1.x <> b1.x AND i1.x <> c1.x AND i1.x <> d1.x AND i1.x <> e1.x AND i1.x <> f1.x AND i1.x <> g1.x AND i1.x <> g2.x AND i1.x <> g3.x AND i1.x <> h1.x AND i1.x <> h2.x AND i1.x <> h3.x

JOIN s i2 ON i2.x <> a2.x AND i2.x <> b2.x AND i2.x <> c2.x AND i2.x <> d2.x AND i2.x <> e2.x AND i2.x <> f2.x AND i2.x <> g1.x AND i2.x <> g2.x AND i2.x <> g3.x AND i2.x <> h1.x AND i2.x <> h2.x AND i2.x <> h3.x AND i2.x <> i1.x

JOIN s i3 ON i3.x <> a3.x AND i3.x <> b3.x AND i3.x <> c3.x AND i3.x <> d3.x AND i3.x <> e3.x AND i3.x <> f3.x AND i3.x <> g1.x AND i3.x <> g2.x AND i3.x <> g3.x AND i3.x <> h1.x AND i3.x <> h2.x AND i3.x <> h3.x AND i3.x <> i1.x AND i3.x <> i2.x

JOIN s i4 ON i4.x <> a4.x AND i4.x <> b4.x AND i4.x <> c4.x AND i4.x <> d4.x AND i4.x <> e4.x AND i4.x <> f4.x AND i4.x <> g4.x AND i4.x <> g5.x AND i4.x <> g6.x AND i4.x <> h4.x AND i4.x <> h5.x AND i4.x <> h6.x AND i4.x <> i1.x AND i4.x <> i2.x AND i4.x <> i3.x

JOIN s i5 ON i5.x <> a5.x AND i5.x <> b5.x AND i5.x <> c5.x AND i5.x <> d5.x AND i5.x <> e5.x AND i5.x <> f5.x AND i5.x <> g4.x AND i5.x <> g5.x AND i5.x <> g6.x AND i5.x <> h4.x AND i5.x <> h5.x AND i5.x <> h6.x AND i5.x <> i1.x AND i5.x <> i2.x AND i5.x <> i3.x AND i5.x <> i4.x

JOIN s i6 ON i6.x <> a6.x AND i6.x <> b6.x AND i6.x <> c6.x AND i6.x <> d6.x AND i6.x <> e6.x AND i6.x <> f6.x AND i6.x <> g4.x AND i6.x <> g5.x AND i6.x <> g6.x AND i6.x <> h4.x AND i6.x <> h5.x AND i6.x <> h6.x AND i6.x <> i1.x AND i6.x <> i2.x AND i6.x <> i3.x AND i6.x <> i4.x AND i6.x <> i5.x

JOIN s i7 ON i7.x <> a7.x AND i7.x <> b7.x AND i7.x <> c7.x AND i7.x <> d7.x AND i7.x <> e7.x AND i7.x <> f7.x AND i7.x <> g7.x AND i7.x <> g8.x AND i7.x <> g9.x AND i7.x <> h7.x AND i7.x <> h8.x AND i7.x <> h9.x AND i7.x <> i1.x AND i7.x <> i2.x AND i7.x <> i3.x AND i7.x <> i4.x AND i7.x <> i5.x AND i7.x <> i6.x

JOIN s i8 ON i8.x <> a8.x AND i8.x <> b8.x AND i8.x <> c8.x AND i8.x <> d8.x AND i8.x <> e8.x AND i8.x <> f8.x AND i8.x <> g7.x AND i8.x <> g8.x AND i8.x <> g9.x AND i8.x <> h7.x AND i8.x <> h8.x AND i8.x <> h9.x AND i8.x <> i1.x AND i8.x <> i2.x AND i8.x <> i3.x AND i8.x <> i4.x AND i8.x <> i5.x AND i8.x <> i6.x AND i8.x <> i7.x

JOIN s i9 ON i9.x <> a9.x AND i9.x <> b9.x AND i9.x <> c9.x AND i9.x <> d9.x AND i9.x <> e9.x AND i9.x <> f9.x AND i9.x <> g7.x AND i9.x <> g8.x AND i9.x <> g9.x AND i9.x <> h7.x AND i9.x <> h8.x AND i9.x <> h9.x AND i9.x <> i1.x AND i9.x <> i2.x AND i9.x <> i3.x AND i9.x <> i4.x AND i9.x <> i5.x AND i9.x <> i6.x AND i9.x <> i7.x AND i9.x <> i8.x

WHERE a1.x = 3

AND a2.x = 5

AND a8.x = 6

AND b4.x = 8

AND b6.x = 7

AND b7.x = 2

AND c4.x = 6

AND c7.x = 4

AND d2.x = 7

AND d5.x = 1

AND d9.x = 2

AND e3.x = 8

AND e4.x = 3

AND e6.x = 2

AND e7.x = 9

AND f1.x = 2

AND f5.x = 8

AND f8.x = 1

AND g3.x = 6

AND g6.x = 3

AND h3.x = 5

AND h4.x = 7

AND h6.x = 1

AND i2.x = 9

AND i8.x = 2

AND i9.x = 6;

## So, how well does this perform?

I ran the query against a bunch of databases (this is not meant to be a scientific test in any way, and the timings were not taken in experimental conditions, but the tests were all carried out using default installs of the database system on the same machine, so I guess the results are fairly relevant.)

Oracle ran the query and returned 1 solution in 32 seconds. Adding a primary key constraint to the table reduced the execution time to 18 seconds.

MS SQL Server ran the query and returned the same solution, but took around 56 minutes to get to the answer. Adding a primary key index reduced this to 35 minutes.

PostgreSQL gave the same solution in around 47 seconds, with a primary key index not having much impact at all.

MySQL failed to get to the answer, as it reported a limit of 61 tables in a join operation.

SQLite also failed to solve the query, giving an error that only 64 tables can be joined in one operation.

DB2 did solve the problem, but took around 35 minutes to get to the solution.

Firebird never gave an error, but eventually after 2 days I got bored and decided to do some work instead of waiting for it to finish.