SQL error
invalid UNION/INTERSECT/EXCEPT ORDER BY clause / 1st ORDER BY term does not match any column in the result set
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
After UNION, INTERSECT or EXCEPT, ORDER BY sorts the combined result, so it can only name its columns (by name or position), not an expression like LENGTH(name). Sort in an outer query, or add the expression as a column.
The exact messages
- PostgreSQLERROR: invalid UNION/INTERSECT/EXCEPT ORDER BY clause
- SQLite1st ORDER BY term does not match any column in the result set
How to read this message
This is what PostgreSQL 18.6 prints for the broken query below, the way psql shows it. LINE gives the line it was reading when it gave up, and the ^ marks the exact spot. DETAIL says more about what went wrong. HINT is PostgreSQL's own suggestion, and it's usually worth following.
ERROR: invalid UNION/INTERSECT/EXCEPT ORDER BY clause
LINE 4: ORDER BY LENGTH(name);
^
DETAIL: Only result column names can be used, not expressions or functions.
HINT: Add the expression/function to every SELECT, or move the UNION into a FROM clause.SQLite 3.54.1 reports the same mistake as 1st ORDER BY term does not match any column in the result set, with no line number: look for the word or column it names.
Why it happens
Sorting by an expression
ORDER BY LENGTH(name) or ORDER BY LOWER(name) after a UNION. The expression isn't a column of the combined result.
Sorting by a column of one side only
A column that appears in only one of the SELECTs doesn't exist in the combined result either.
Reproduce it, then fix it
Both queries run on a real database in your browser. Switch dialects to compare.
SELECT name FROM customers
UNION
SELECT name FROM staff
ORDER BY LENGTH(name);SELECT name FROM (
SELECT name FROM customers
UNION
SELECT name FROM staff
) everyone
ORDER BY LENGTH(name), name;Wrap the UNION in a subquery; the outer query may sort by any expression. The second sort key, name, makes ties come out in a fixed order.
The fix returns, PostgreSQL: first 6 of 18 rows
| name |
|---|
| Noah Kim |
| Zara Ali |
| Rosa Vega |
| Ben Carter |
| Ellie Park |
| Jonah Reed |
The fix returns, SQLite: first 6 of 18 rows
| name |
|---|
| Noah Kim |
| Zara Ali |
| Rosa Vega |
| Ben Carter |
| Ellie Park |
| Jonah Reed |
Experiment with it: PostgreSQL playground·SQLite playground
Another fix
Or make the expression a column of both SELECTs, then sort by its name.
SELECT name, LENGTH(name) AS len FROM customers
UNION
SELECT name, LENGTH(name) FROM staff
ORDER BY len, name;Returns, PostgreSQL: first 6 of 18 rows
| name | len |
|---|---|
| Noah Kim | 8 |
| Zara Ali | 8 |
| Rosa Vega | 9 |
| Ben Carter | 10 |
| Ellie Park | 10 |
| Jonah Reed | 10 |
Returns, SQLite: first 6 of 18 rows
| name | len |
|---|---|
| Noah Kim | 8 |
| Zara Ali | 8 |
| Rosa Vega | 9 |
| Ben Carter | 10 |
| Ellie Park | 10 |
| Jonah Reed | 10 |
Official documentation
Stop hitting this error
Practise the concept behind it in a five-minute lesson, or read the full guide: SQL UNION vs UNION ALL, INTERSECT and EXCEPT.