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

Article translations Article translations
Article ID: 246201 - View products that this article applies to.
This article was previously published under Q246201
BUG #: 56519 (SQLBUG_70)
Expand all | Collapse all

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.

Properties

Article ID: 246201 - Last Review: March 14, 2006 - Revision: 2.1
APPLIES TO
  • Microsoft SQL Server 7.0 Standard Edition
Keywords: 
kbbug kbfix KB246201

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com