A database of all combinations of 39 numbers taken 5 at a time, total combinations = 575757 I want to create a numerical database of all combinations of 39 integers(1 Through 39) taken 5 at a time. I know the total possible combinations = 575757. This would be the size of the database.

I would then like to query the database for various criteria, e.g. number of combinations that are made up of all even numbers, all odd numbers, 1 even, 1 odd, 2 even, 2 odd, etc,  and then query those results for other criteria such as restricted sums of all integers, and range distribution.

How would I do this? Obviously this is a Lottery oriented idea and I expect nothing but ridicule and disdain.
Ridicule and disdain aside, the following query would return all possible permutations of your integers.

SELECT n1.i, n2.i, n3.i, n4.i, n5.i
FROM numbers AS n1
INNER JOIN numbers AS n2 ON n1.i&lt;n2.i
INNER JOIN numbers AS n3 ON n2.i&lt;n3.i
INNER JOIN numbers AS n4 ON n3.i&lt;n4.i
INNER JOIN numbers AS n5 ON n4.i&lt;n5.i


.. given that the table or common table expression numbers contains 39 unique records with 1&lt;=i&lt;=39.

You could add different filters for your purposes, like how many odd numbers:

WHERE (CASE WHEN n1.i%2=1 THEN 1 ELSE 0 END)+
      (CASE WHEN n2.i%2=1 THEN 1 ELSE 0 END)+
      (CASE WHEN n3.i%2=1 THEN 1 ELSE 0 END)+
      (CASE WHEN n4.i%2=1 THEN 1 ELSE 0 END)+
      (CASE WHEN n5.i%2=1 THEN 1 ELSE 0 END)&gt;howmanyoddnums


.. or even numbers (WHEN nx.i%2=0 THEN ...). And because the numbers are "ordered" with the &lt; in the join, n1.i will always be the lowest and n5.i the highest.

Any more advanced features (or tuning for that matter) will come down to what platform and dialect of SQL you're using.