Skip to content Skip to sidebar Skip to footer

Sql: Is Results Order Mapped To Values In 'in' Expression?

I have a list of record IDs for which I want to retrieve a single value from a table in SQL Server 2008. My query is: SELECT TestResult.FailureDetails FROM TestResult WHERE TestRes

Solution 1:

No, you will not always get results in order based on the IN.

Be explicit, and add an ORDER BY clause.

SELECT FailureDetails 
FROM TestResult 
WHERE ID IN (1,2,3,...) 
ORDERBY ID;

Solution 2:

I would be surprised if they were ordered by the IN clause. As you've got no ORDER BY you'll probably get the results in PK order, but I wouldn't depend on that either.

If order is important you should set it explicitly:

SELECT TestResult.ID, TestResult.FailureDetails FROM TestResult WHERE TestResult.ID IN (1,2,3,...)
ORDERBY TestResult.ID

Post a Comment for "Sql: Is Results Order Mapped To Values In 'in' Expression?"