SQL Server Error 1062: TOP N WITH TIES Fix
In this article
SQL Server error 1062 occurs when you use the TOP N WITH TIES clause without an ORDER BY clause. The fix is simple: add an ORDER BY clause that matches the column list in your TOP N WITH TIES expression. Without ORDER BY, SQL Server cannot determine which rows are tied, so it raises error 1062. This guide walks you through the cause, the correct syntax, and how to avoid related mistakes.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or another client that can run T-SQL queries.
- Permission to run SELECT queries against the target database.
- The query that is currently producing error 1062.
Step-by-Step
- Identify the failing query. Look at the error message. It will say something like: “The TOP N WITH TIES clause is not allowed without a corresponding ORDER BY clause.” The query will contain TOP (n) WITH TIES but no ORDER BY.
- Decide which column or columns define the “ties.” WITH TIES returns extra rows that have the same values in the ORDER BY columns as the last row in the TOP n set. So you must choose an ORDER BY that makes sense for your business logic. For example, if you want the top 5 highest‑priced products and include ties, ORDER BY Price DESC.
- Add the ORDER BY clause to your query. Place it after the FROM and WHERE clauses, before any other clauses like OFFSET or FOR XML. For example:
This returns the 5 most expensive products plus any additional products with the same price as the 5th product.SELECT TOP (5) WITH TIES ProductName, Price FROM Products ORDER BY Price DESC; - Verify that the ORDER BY column list is deterministic enough. If multiple rows share the same ORDER BY values, WITH TIES may return more rows than you expect. That is the intended behavior. If you need exactly n rows, remove WITH TIES and use a plain TOP (n).
- Run the corrected query. Execute it in SSMS or your client. The error should disappear. Check the result set to confirm that ties are included as expected.
- If you are using a subquery or common table expression (CTE), the ORDER BY must be inside the same SELECT that uses TOP … WITH TIES. For example:
The ORDER BY is required in the inner query, not the outer one.WITH TopProducts AS ( SELECT TOP (3) WITH TIES Category, ProductName, Price FROM Products ORDER BY Price DESC ) SELECT * FROM TopProducts; - Check for compatibility level. WITH TIES is supported in all modern SQL Server versions. If you are on a very old version (SQL Server 2000 or earlier), the syntax may differ. For current versions, the rule is consistent: ORDER BY is mandatory.
- Test with a small dataset. If you are unsure how many rows will be returned, run the query without TOP first to see the distribution of values in your ORDER BY columns. Then add TOP WITH TIES and compare.
Common Mistakes
- Using ORDER BY in the outer query only. When TOP … WITH TIES is inside a subquery or CTE, the ORDER BY must be in that same SELECT. An ORDER BY on the outer query does not satisfy the requirement.
- Assuming WITH TIES returns exactly n rows. It returns at least n rows, plus any rows tied with the last row. If you need a fixed number, use TOP (n) without WITH TIES.
- Using a non‑deterministic ORDER BY. If the ORDER BY column has duplicate values, the “last row” of the top n set may be ambiguous, and the number of tied rows can vary between executions. Add a unique tiebreaker column (like an ID) to make results stable.
- Forgetting that WITH TIES requires parentheses around the number. The correct syntax is TOP (n) WITH TIES, not TOP n WITH TIES. The parentheses are required when using WITH TIES.
- Placing ORDER BY after OFFSET or FOR XML. The ORDER BY clause must come before those clauses. Check the clause order in your query.
Tips
- Always include a unique column in the ORDER BY if you want predictable results. For example, ORDER BY Price DESC, ProductID.
- Use WITH TIES when you want to include all rows that share the same ranking value, such as top scores or highest prices.
- If you are porting queries from another database, note that some systems use different syntax for “top with ties” (e.g., LIMIT … WITH TIES in PostgreSQL). SQL Server requires ORDER BY.
- Keep your queries readable by formatting the ORDER BY on its own line.
- When debugging, run the query without TOP to see the full sorted result set, then decide how many rows you actually need.
Troubleshooting
If you still get error 1062 after adding ORDER BY, check these points:
- Is the ORDER BY in the correct SELECT? For nested queries, the ORDER BY must be in the same SELECT statement that contains TOP … WITH TIES.
- Are you using a view or inline function? Some constructs do not allow ORDER BY unless TOP is also present. In a view, ORDER BY is only allowed with TOP, OFFSET, or FOR XML. If you have TOP … WITH TIES, ORDER BY is allowed and required.
- Is the error actually 1062? Double‑check the message. Error 1062 is specific to TOP WITH TIES without ORDER BY. Other errors may have similar numbers but different causes.
- Check for syntax errors around the TOP clause. Missing parentheses or a misplaced WITH TIES can cause parser errors that look like 1062. Ensure the syntax is exactly TOP (n) WITH TIES.
- If you are using dynamic SQL, verify that the ORDER BY is included in the generated string. Print the string before executing to inspect it.
If none of these resolve the issue, simplify the query to the smallest version that still fails, then add clauses back one by one to isolate the problem.
Frequently Asked Questions
What does “WITH TIES” do in SQL Server?
WITH TIES returns any rows that have the same values in the ORDER BY columns as the last row included by TOP (n). It can return more than n rows.
Can I use TOP WITH TIES without ORDER BY?
No. SQL Server requires an ORDER BY clause when you use TOP … WITH TIES. Omitting it raises error 1062.
Does WITH TIES work with OFFSET‑FETCH?
No. WITH TIES is only valid with TOP. OFFSET‑FETCH does not support WITH TIES.
Why does my query return more rows than the number I specified?
That is the intended behavior of WITH TIES: it includes all tied rows. If you need exactly n rows, remove WITH TIES.
Is error 1062 the same as error 1062 in other databases?
No. Error numbers are specific to SQL Server. Always check the message text for the exact cause.
Can I use a variable for the number in TOP (n) WITH TIES?
Yes, you can use a variable or expression inside the parentheses, e.g., TOP (@n) WITH TIES, as long as you also have ORDER BY.