Domanda

Running the following query returns 4 rows. As I can see in SSMS the order of returned rows is the same as I specified in the IN clause.

SELECT * FROM Table WHERE ID IN (4,3,2,1)

Can I say that the order of returned rows are ALWAYS the same as they appear in the IN clause?

If yes then is it true, that the following two queries return the rows in the same order? (as I've tested the orders are the same, but I don't know if I can trust this behavior)

SELECT TOP 10 * FROM Table ORDER BY LastModification DESC

SELECT * FROM Table WHERE ID IN (SELECT TOP 10 ID FROM Table ORDER BY LastModification DESC)

I ask this question because I have a quite complex select query. Using this trick over it brings me ca. 30% performance gain, in my case.

È stato utile?

Soluzione

You cannot guarantee the records to be in any particular order unless you use ORDER BY clause. You may use some tricks that may work some of the time but they won't give you guarantee of the order.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top