TOP is a SQL Server feature used to restrict the number or percentage of rows returned by a query.
SQL Server users frequently use TOP when retrieving a limited number of rows from large joined datasets.
Return the ten highest-value orders with customer information.
SELECT TOP 10
c.CustomerName,
o.OrderID,
o.OrderAmount
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
ORDER BY o.OrderAmount DESC;
TOP is useful in SQL Server reporting, dashboards, testing, and top-performance analysis.
- TOP is commonly used in SQL Server.
- It limits the number of returned rows.
- ORDER BY determines which rows are considered top.
- TOP can be combined with JOIN.
- Other databases may use different syntax.
What does TOP do in SQL Server?
TOP limits the number or percentage of rows returned by a query.
Why should TOP usually be combined with ORDER BY?
ORDER BY defines which records should be considered the top records.