SeyoS SeyoS - 9 days ago 4
SQL Question

Is the order of selection parameters important?

If i use a table containing at least 2 columns like this :


Assuming that there is a huge amount of data and that there is much more unique result in 'FirstName' than in 'LastName', which query is the fastest :

Query 1 :

SELECT * USER WHERE LastName = 'Dupont' AND FirstName = 'Robert'

Query 2:

SELECT * USER WHERE FirstName = 'Robert' AND LastName = 'Dupont'

Is there a difference ?

Thanks for your help !


The order of the comparisons does not matter. SQL Server should optimize the WHERE clause for the data.

However, if you care about performance, you should use an index, either user(firstname, lastname) or user(lastname, firstname). The order of comparisons is not the right thing to be thinking about.