A TOP-N query sorts data according to a chosen metric and returns only the required number of highest or lowest records.
Top-N analysis is a common business requirement for identifying best customers, highest sales, lowest prices, or top-performing products.
Find the five employees with the highest salaries.
SELECT
EmployeeName,
Salary
FROM Employees
ORDER BY Salary DESC
FETCH FIRST 5 ROWS ONLY;
TOP-N queries are widely used in sales leaderboards, customer analysis, product rankings, financial analysis, and management reporting.
- TOP-N returns a limited number of ranked rows.
- ORDER BY determines the ranking.
- DESC is commonly used for highest values.
- ASC can be used for lowest values.
- Different databases use different row-limiting syntax.
How do you find the top five salaries?
Sort salary in descending order and limit the result to five rows.
Why is ORDER BY required for a TOP-N query?
ORDER BY determines which rows qualify as the highest or lowest N records.