SQL Server Error 1033: Fix Invalid ORDER BY Clause
In this article
SQL Server error 1033 means your query uses ORDER BY in a place where it is not allowed, most often inside a view, inline function, subquery, or a UNION branch without a TOP or OFFSET/FETCH. The fix is to remove the ORDER BY from the inner query and apply it only to the outermost SELECT, or add TOP (or OFFSET/FETCH) if you truly need ordered rows inside a view or subquery. The exact message is "The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified."
What You'll Need
- Access to SQL Server Management Studio (SSMS), Azure Data Studio, or another client that can run and edit queries.
- The query or view definition that is throwing error 1033.
- Permission to ALTER the view or function if the error comes from a stored object.
Step-by-Step
- Find the exact statement. In SSMS, run the failing query and read the message pane. Note the line number and whether the error comes from an ad-hoc query or from a view/function. If it comes from a view, run
sp_helptext 'YourViewName'or open the view in Object Explorer to see its definition. - Locate every ORDER BY. Search the definition for
ORDER BY. Error 1033 appears when ORDER BY sits inside a view, an inline table-valued function, a derived table (a subquery in the FROM clause), a subquery in WHERE/SELECT, or a common table expression (CTE). - Remove ORDER BY from the inner query. Delete the ORDER BY clause from the view, subquery, or CTE body. Sorting inside these objects has no guaranteed effect on the outer result anyway, because SQL Server is free to reorder rows unless the outermost query asks for an order.
- Apply ORDER BY to the outermost SELECT. Put the ORDER BY at the end of the statement that calls the view or subquery, for example
SELECT * FROM dbo.MyView ORDER BY CreatedDate DESC;. This is the correct place for sorting. - If you must keep ORDER BY inside the object, add TOP or OFFSET/FETCH. For a view or inline function, change it to something like
SELECT TOP (100) PERCENT ... ORDER BY ...or useORDER BY ... OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY. The TOP or OFFSET/FETCH makes the ORDER BY legal. Be aware that TOP (100) PERCENT does not guarantee order to the caller; it only satisfies the parser. - Check UNION and UNION ALL branches. In a UNION, ORDER BY is only allowed at the very end of the whole statement, not inside individual branches. Move it to the end, or wrap each branch in a subquery with TOP if you need per-branch limits.
- Save and test. For a view, run
ALTER VIEWwith the corrected definition, or right-click the view in Object Explorer and choose Design, then save. Re-run the original query and confirm the error is gone and the rows come back in the expected order.
Common Mistakes
- Assuming ORDER BY inside a view controls the order callers see. It does not, even when it is made legal with TOP.
- Adding TOP (100) PERCENT and expecting sorted output. The outer query still needs its own ORDER BY.
- Leaving ORDER BY inside only one branch of a UNION. The parser rejects it; move it to the end of the whole statement.
- Forgetting that ORDER BY in a subquery used with IN or EXISTS is also invalid unless TOP or OFFSET/FETCH is present.
- Editing the view in one database while the query runs against another. Confirm the database context in the SSMS toolbar before altering.
Tips
- Treat views as reusable row sources, not as sorted reports. Sort in the report, application, or outer query.
- Use OFFSET/FETCH instead of TOP when you need paging; it is the modern, supported pattern and makes ORDER BY legal in the same clause.
- When debugging, comment out sections of a long query to isolate which subquery contains the offending ORDER BY.
- Keep a copy of the original view definition before altering it, so you can roll back quickly.
Troubleshooting
If the error persists after removing ORDER BY from the inner query, check for nested views: the ORDER BY may live in a view that your view calls. Run sp_helptext on each object in the chain. If the error appears only in a stored procedure, confirm the procedure is not wrapping a view that still contains ORDER BY. If you cannot alter the view because of permissions, ask a database administrator to apply the change; do not attempt to work around permissions. If the query runs but returns rows in an unexpected order, remember that without an outer ORDER BY, SQL Server does not guarantee row order at all, even if the data looks sorted in a small test.
Frequently Asked Questions
What exactly does SQL Server error 1033 mean?
It means ORDER BY was used in a context where SQL Server does not allow it unless TOP, OFFSET/FETCH, or FOR XML is also present: views, inline functions, derived tables, subqueries, and CTEs.
Can I just add TOP 100 PERCENT to make it work?
Yes, it makes the syntax legal, but it does not guarantee the caller receives rows in that order. Put ORDER BY on the outermost query instead.
Why does ORDER BY work in a normal SELECT but not in a view?
A view is a stored row source, not a result set with a defined order. SQL Server only guarantees order at the outermost SELECT, so it rejects ORDER BY inside the view unless TOP or OFFSET/FETCH is specified.
Does ORDER BY work inside a UNION?
Only at the end of the entire UNION statement. Individual branches cannot contain ORDER BY unless they are wrapped in a subquery with TOP or OFFSET/FETCH.
Will removing ORDER BY from my view break existing reports?
It should not, because the view never guaranteed order. Any report that relied on the view's internal ORDER BY was already relying on undefined behavior. Add ORDER BY to the report query itself.
Is error 1033 the same as error 1033 in other database systems?
No. Error numbers are specific to SQL Server. The same number in another database product may mean something completely different.