TOP Clause — Complete Guide
TOP Clause — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of SQL Server Tutorial on Toolliyo Academy.
On this page
SQL Server Tutorial · Lesson 17 of 100
TOP Clause
SQL basics → Queries → Advanced
SQL basics · 1 — SELECT · ~6 min · SQL — SQL Queries & Clauses
What is this?
TOP limits how many rows come back. TOP (n) or TOP (n) PERCENT. Often paired with ORDER BY so “top” means something.
Why should you care?
Leaderboards and “latest 10 orders” must not stream the whole table to the app.
See it live — copy this example
Run in SQL Server Management Studio (SSMS) or Azure Data Studio.
USE DataVerse;
SELECT TOP (3) OrderId, City, Amount
FROM dbo.Orders
ORDER BY Amount DESC;
What happened?
- After sorting by Amount descending, only the three highest orders return.
- Without ORDER BY, TOP is arbitrary.
Practice next
- Run TOP (3) with ORDER BY Amount DESC.
- Try TOP (50) PERCENT and note the count.
- Add WITH TIES after TOP (2) ORDER BY Amount DESC and compare.
- TOP (1) WITH TIES ORDER BY Amount DESC.
- TOP (2) ORDER BY OrderId DESC for “latest ids”.
Remember
TOP caps row count. Pair TOP with ORDER BY. WITH TIES keeps equal ranks when needed.
Highest value orders widget
Admin home shows TOP 5 DataVerse orders by Amount.
Outcome: Fraud and VIP review starts from the biggest tickets.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!