A database of all combinations of 39 numbers taken 5 at a time, total combinations = 575757
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.