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

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

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

When writing T-SQL code, at some point, under certain circumstances, you might get the error message: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

But let’s take things from  the beginning with the use of an example.

First, let’s create a temporary table:

CREATE TABLE #tmpTable
    (
        id INT PRIMARY KEY ,
        code VARCHAR(50) ,
        age INT
    );

Then, let’s populate the table with some sample data:

INSERT INTO #tmpTable
VALUES ( 1, 'code 1', 25 ) ,
       ( 2, 'code 2', 27 ) ,
       ( 3, 'code 3', 29 ) ,
       ( 4, 'code 4', 25 ) ,
       ( 5, 'code 5', 21 ),
       ( 6, 'code 5', 21 );

Now let’s run the below query, which tries to retrieve the distinct “age” values along with sorting the results based on the id:

SELECT   DISTINCT age
FROM     #tmpTable
ORDER BY code;

However, when we execute the above query, we get the error message:

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

As the error message indicates, you are using in the ORDER BY statement, a column which is not included in the SELECT list. In this case this column is “code”.

Therefore, you definitely need to include in your query the column “code”.

So, for this example, you can try running the query as below:

SELECT   DISTINCT age,code
FROM     #tmpTable
ORDER BY code;

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

 

Other SQL Server troubleshooting articles to check on SQLNetHub:

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (11 votes, average: 5.00 out of 5)

Loading...

Join SQLNetHub!

Make sure you check out SQLArtBits for useful SQL Server tools!

Reference: SQLNetHub (https://www.sqlnethub.com)

© 2018 SQLNetHub


Recommended eBooks on SQL Server:

Developing with SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Developing SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Artemakis Artemiou
Artemakis Artemiou is a Senior SQL Server Architect, Author, Software Developer and a Microsoft Data Platform MVP. He has over 15 years of experience in the IT industry in various roles. Among other, via his initiative SQLEBooks.com, Artemakis authors and publishes eBooks on different topics on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Additionally he is the founder of the SQLArtBits initiative that aims to provide the technical community with simple, yet powerful and high-quality SQL Server tools. Currently, the highlights of these tools are DBA Security Advisor and In-Memory OLTP Simulator. Artemakis's official website can be found at aartemiou.com. Artemakis's blogs can be found at: SQLNetHub.com and TechHowTos.com.