Eight Queens

From John Senner
Jump to navigation Jump to search
-- SQL Eight Queens in SQL

-- Create the board:
CREATE TABLE rows (
  id integer PRIMARY KEY
);
INSERT INTO rows (id) VALUES (1), (2), (3), (4), (5), (6), (7), (8);

CREATE TABLE cols (
  id integer PRIMARY KEY
);
INSERT INTO cols (id) VALUES (1), (2), (3), (4), (5), (6), (7), (8);

-- Get a set of queens:
SELECT
  cols.id AS col1, rows.id AS row1,
  col2, row2,
  col3, row3,
  col4, row4,
  col5, row5,
  col6, row6,
  col7, row7,
  col8, row8
FROM rows, cols, (SELECT
  col3, row3,
  col4, row4,
  col5, row5,
  col6, row6,
  col7, row7,
  col8, row8,
  rows.id AS row2, cols.id AS col2
  FROM rows, cols, (SELECT
    col4, row4,
    col5, row5,
    col6, row6,
    col7, row7,
    col8, row8,
    rows.id AS row3, cols.id AS col3
    FROM rows, cols, (SELECT
      col5, row5,
      col6, row6,
      col7, row7,
      col8, row8,
      rows.id AS row4, cols.id AS col4
      FROM rows, cols, (SELECT
        col6, row6,
        col7, row7,
        col8, row8,
        rows.id AS row5, cols.id AS col5
        FROM rows, cols, (SELECT
          col7, row7,
          col8, row8,
          rows.id AS row6, cols.id AS col6
          FROM rows, cols, (SELECT
            col8, row8,
            rows.id AS row7, cols.id AS col7
            FROM rows, cols
            WHERE cols.id != col8 AND rows.id != row8 -- This checks rook moves
            AND (cols.id + rows.id != col8 + row8) -- This checks bishop moves
            AND (cols.id - rows.id != col8 - row8)
          ) AS b7
          WHERE cols.id != col8 AND rows.id != row8
          AND cols.id != col7 AND rows.id != row7
          AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
          AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
        ) AS b6
        WHERE cols.id != col8 AND rows.id != row8
        AND cols.id != col7 AND rows.id != row7
        AND cols.id != col6 AND rows.id != row6
        AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
        AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
        AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
      ) AS b5
      WHERE cols.id != col8 AND rows.id != row8
      AND cols.id != col7 AND rows.id != row7
      AND cols.id != col6 AND rows.id != row6
      AND cols.id != col5 AND rows.id != row5
      AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
      AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
      AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
      AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
    ) AS b4
    WHERE cols.id != col8 AND rows.id != row8
    AND cols.id != col7 AND rows.id != row7
    AND cols.id != col6 AND rows.id != row6
    AND cols.id != col5 AND rows.id != row5
    AND cols.id != col4 AND rows.id != row4
    AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
    AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
    AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
    AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
    AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
  ) AS b3
  WHERE cols.id != col8 AND rows.id != row8
  AND cols.id != col7 AND rows.id != row7
  AND cols.id != col6 AND rows.id != row6
  AND cols.id != col5 AND rows.id != row5
  AND cols.id != col4 AND rows.id != row4
  AND cols.id != col3 AND rows.id != row3
  AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
  AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
  AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
  AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
  AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
  AND (cols.id + rows.id != col3 + row3) AND (cols.id - rows.id != col3 - row3)
) AS b2
WHERE cols.id != col8 AND rows.id != row8
AND cols.id != col7 AND rows.id != row7
AND cols.id != col6 AND rows.id != row6
AND cols.id != col5 AND rows.id != row5
AND cols.id != col4 AND rows.id != row4
AND cols.id != col3 AND rows.id != row3
AND cols.id != col2 AND rows.id != row2
AND (cols.id + rows.id != col8 + row8) AND (cols.id - rows.id != col8 - row8)
AND (cols.id + rows.id != col7 + row7) AND (cols.id - rows.id != col7 - row7)
AND (cols.id + rows.id != col6 + row6) AND (cols.id - rows.id != col6 - row6)
AND (cols.id + rows.id != col5 + row5) AND (cols.id - rows.id != col5 - row5)
AND (cols.id + rows.id != col4 + row4) AND (cols.id - rows.id != col4 - row4)
AND (cols.id + rows.id != col3 + row3) AND (cols.id - rows.id != col3 - row3)
AND (cols.id + rows.id != col2 + row2) AND (cols.id - rows.id != col2 - row2)
LIMIT 1337; --Arbitrary, you can let yours go all day.
-- Note that this won't return very many unique solutions (unless your queens have numbers written on them)