Does the order of conditions matter if we have more than one in a query?

Question

Does the order of conditions matter if we have more than one in a SQL query?

Answer

No, the order of conditions within a query should not matter, and will not affect the result set.

If you use the same conditions following the WHERE clause and combined using AND in any order, then the query will always return the intersection of all the conditions.

For example, the following queries will return the same results,

SELECT *
FROM table
WHERE condition_1
AND condition_2;

SELECT *
FROM table
WHERE condition_2
AND condition_1;
1 Like

What about performance?

2 Likes

Yes, the order of conditions in a where clause can impact the performance if you are working with very large tables. There are numerous factors that come into play. Some basic factors would be the number of rows, the indexes and the number of tables.

2 Likes