SELECT word FROM words WHERE length(word) = N AND word like '%A%A%' (if there are 2 'A' in the orig) AND word like '%B%' (if there is 1 'B' in the orig) ... ...