LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1917 min read

TOP Clause

Learn how to limit the number of rows returned in SQL Server using the TOP clause, including TOP PERCENT and combining TOP with ORDER BY.

Introduction

Sometimes you don't want every matching row — you just want the first few, such as the 5 highest-paid employees or the 10 most recent orders. SQL Server provides the TOP clause for exactly this purpose. Unlike MySQL, which uses LIMIT, SQL Server uses TOP directly after SELECT. This lesson covers TOP's syntax and its common variations.

What You Will Learn
  • The syntax of the TOP clause.
  • How TOP differs from MySQL's LIMIT.
  • How to combine TOP with ORDER BY for meaningful "top N" queries.
  • How TOP PERCENT and TOP WITH TIES work.

Basic TOP Syntax

TOP appears immediately after SELECT and specifies the maximum number of rows to return. It can optionally be wrapped in parentheses, which is required if you use an expression instead of a literal number.

Basic TOP
SELECT TOP 3 FirstName, Department, Salary
FROM Employees;
Result

Click Run to see what this code prints.

TOP Without ORDER BY is Unpredictable

Without an ORDER BY clause, TOP returns whatever rows SQL Server happens to encounter first internally — this is not guaranteed to be meaningful or consistent between runs.

TOP vs LIMIT

Developers coming from MySQL or PostgreSQL often look for a LIMIT keyword — SQL Server does not support LIMIT (except in some newer compatibility contexts within Azure SQL). Instead, TOP is placed right after SELECT, not at the end of the query, and it does not support an "offset" directly the way LIMIT with OFFSET does. For paging through results, SQL Server instead uses OFFSET ... FETCH NEXT alongside ORDER BY.

FeatureMySQLSQL Server
Limit row countLIMIT 5SELECT TOP 5
Position in queryEnd of queryRight after SELECT
Percentage of rowsNot built-inTOP 10 PERCENT
Paging (skip + take)LIMIT 10 OFFSET 20OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY

TOP with ORDER BY

To get a meaningful "top N" result — such as the highest-paid employees — always pair TOP with ORDER BY. SQL Server first sorts the full result set, then takes the requested number of rows from the top.

Top 3 Highest Salaries
SELECT TOP 3 FirstName, Salary
FROM Employees
ORDER BY Salary DESC;
Result

Click Run to see what this code prints.

TOP PERCENT

Instead of a fixed row count, TOP PERCENT returns a percentage of the total rows in the result set, rounded up to the nearest whole number of rows.

TOP PERCENT
SELECT TOP 40 PERCENT FirstName, Salary
FROM Employees
ORDER BY Salary DESC;
Result

Click Run to see what this code prints.

TOP WITH TIES

By default, TOP cuts off strictly at the requested row count, even if that splits a group of tied values. TOP ... WITH TIES includes any additional rows that tie with the last row in the result, which requires ORDER BY to determine what counts as a tie.

TOP WITH TIES
SELECT TOP 2 WITH TIES FirstName, Salary
FROM Employees
ORDER BY Salary DESC;
Result

Click Run to see what this code prints.

Common Mistakes

Avoid These Mistakes
  • Trying to use LIMIT in SQL Server T-SQL — it is not valid syntax.
  • Using TOP without ORDER BY and assuming the results are meaningfully sorted.
  • Forgetting parentheses around TOP when using a variable or expression, e.g. SELECT TOP (@n).
  • Expecting TOP PERCENT to return an exact fractional row count — it always rounds up.

Best Practices

  • Always pair TOP with ORDER BY when the specific rows returned matter.
  • Use OFFSET ... FETCH NEXT instead of TOP when you need paging (skip + take) behavior.
  • Use parentheses around the TOP expression when using a variable, e.g. TOP (@RowCount).
  • Reach for TOP WITH TIES when tied boundary values should all be included, such as reporting all employees tied for the highest salary.

Frequently Asked Questions

No, standard T-SQL does not support the LIMIT keyword. Use TOP for simple row limiting, or OFFSET ... FETCH NEXT for paging.

Yes. TOP can limit how many rows an UPDATE or DELETE statement affects, which is useful for batched cleanup operations, though it should be paired with ORDER BY logic carefully since UPDATE/DELETE with TOP alone does not support ORDER BY directly.

TOP simply returns the first N rows of the result set. OFFSET/FETCH lets you skip a number of rows first and then fetch a page of rows after that, making it suitable for pagination.

Key Takeaways

  • TOP limits the number of rows a query returns.
  • SQL Server uses TOP instead of MySQL's LIMIT, placed right after SELECT.
  • Always combine TOP with ORDER BY for predictable results.
  • TOP PERCENT returns a percentage of the total rows, rounded up.
  • TOP WITH TIES includes rows tied with the last qualifying row.

Summary

TOP is SQL Server's tool for limiting result sets, and it becomes especially powerful when combined with ORDER BY. Next, you will learn how to remove duplicate rows from your results using DISTINCT.

Next Lesson →

DISTINCT