ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

Error Message:
Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

Severity level:
15.

Description:
This error message appears when you try to execute a SELECT DISTINCT Statement that should be sorted by a column, which does not appear in the SELECT list.

Consequences:
The SQL statement can be parsed, but cannot be executed and execution is stopped.

Resolution:
Error of the severity level 15 are generated by the user and can be fixed by the SQL Server user.

Versions:
All versions of SQL Server.

Example(s):
SELECT DISTINCT CustomerID
  FROM Northwind.dbo.Orders
 ORDER BY OrderID

Remarks:
The above example tries to sort by the column OrderID. Because the keyword DISTINCT is also specified, either the column OrderID must appear in the SELECT list or the resultset must be sorted by CustomerID.

]]>

Leave a comment

Your email address will not be published.