FIX: Subquery with ORDER BY may Return Results in Wrong Order (246201)



The information in this article applies to:

  • Microsoft SQL Server 7.0

This article was previously published under Q246201
BUG #: 56519 (SQLBUG_70)

SYMPTOMS

The ORDER BY clause of a SELECT statement may be ignored when the sub-select statement also contains an ORDER BY clause. For example, the following query returns results in the wrong order:
use pubs
select * 
FROM (select top 3 * from authors
     where au_lname < 'MacFeather'
     order by au_lname desc) as a
order by au_lname
				
The three results rows should be ordered "Hunter", "Karsen", "Locksley" but the results are reversed.

WORKAROUND

Instead of using "*" to return all columns in the table, specify the column names explicitly. The following query works as expected:
use pubs
select au_id, au_lname, au_fname, phone, address, city, state, zip, contract 
FROM (select top 3 * from authors
     where au_lname < 'MacFeather'
     order by au_lname desc) as a
order by au_lname
				

STATUS

Microsoft has confirmed this to be a problem in SQL Server 7.0. This problem has been corrected in U.S. Service Pack 2 for Microsoft SQL Server 7.0. For more information, click the following article number to view the article in the Microsoft Knowledge Base:

254561 INF: How to Obtain Service Pack 2 for Microsoft SQL Server 7.0 and Microsoft Data Engine (MSDE) 1.0

For more information, contact your primary support provider.

Modification Type:MajorLast Reviewed:3/14/2006
Keywords:kbbug kbfix KB246201