SQL – WHERE (X, Y) IN (A, B)

Why not something simple like the following?

WHERE (X = 1 AND Y = 2) OR (X = 5 AND Y = 6) ...

Or, if you’re looking for rows (based on your example) where Y should be X + 1, then:

WHERE Y = X + 1

If you have thousands of OR clauses like the above, then I would suggest you populate a criterion table ahead of time, and rewrite your query as a join. Suppose you have such a table Criteria(X, Y) then your query becomes much simpler:

SELECT Table.*
FROM Table
    INNER JOIN Criteria ON Table.X = Criteria.X AND Table.Y = Criteria.Y

Don’t forget to add an index / foreign keys as necessary to the new table.

If for some reason you prefer to not create a table ahead of time, you can use a temporary table or table variable and populate it within your procedure.

Leave a Comment