ORDER BY items must appear in the select list if SELECT DISTINCT is specified
With DISTINCT, the database can only sort by columns that are part of the selected result.
ORDER BY items must appear in the select list if SELECT DISTINCT is specifiedfor SELECT DISTINCT, ORDER BY expressions must appear in select listORDER BY clause is not in SELECT list, references column which is not in SELECT listDISTINCT ORDER BY error Meaning
DISTINCT collapses rows, so ordering by a column that isn't selected is ambiguous. Add the column to the select list, use GROUP BY with an aggregate (MAX(col)), or wrap the DISTINCT query in a subquery and sort outside.
Common causes
- ORDER BY column not in the DISTINCT select list
- Expression in ORDER BY not in SELECT
- Joined table column used for sorting
- ORM adding DISTINCT automatically
- Mixed alias and column names
⚡ Quick fix
- Add the ORDER BY column to the SELECT list
- Use
GROUP BYwithMIN/MAXfor sorting - Sort in an outer query
Detailed fix by platform
SQL
- sql
SELECT DISTINCT name FROM t ORDER BY name; SELECT name, MAX(created_at) AS last_seen FROM t GROUP BY name ORDER BY last_seen DESC;
How to diagnose
- DISTINCT — Used?
- Sort column — In SELECT?
- ORM — Adds DISTINCT?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- 1248 MySQL ERROR 1248 (42000): Every derived table must have its own alias
- 8120 SQL Server Msg 8120: Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
- cartesian Query returns far too many rows: accidental Cartesian product (missing JOIN condition)
Most viewed in SQL
Other ways to find it
Where this error sits
Origin: Data. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 18 Nov 2026