FYI, SQL queries do have a maximum length, and you may crap out your query if you specify too many tens of thousands of items inside the query. It's also n squared efficiency to run, since it has to compare every item specified against every account in each of the tables without using an index. Depending on how many items are in each of those tables, or how often this needs to run, there may be a much better way to do it.