Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

TopN Queries

James Taylor edited this page May 16, 2013 · 4 revisions

A common use case with SQL is to identify and return the top N rows, where top is determined by an ORDER BY clause and N is defined by a LIMIT clause. For example:

SELECT company FROM Fortune500
WHERE location="US"
ORDER BY revenue DESC
LIMIT 10

This query will return the top 10 companies with the highest revenue. The execution of this query still needs to make a pass through all the rows (restricted by your WHERE clause), but it can perform this on the server side and only hold on to the top 10 results it sees. This should be pretty fast (as compared with having to sort the entire list). Check out our performance numbers here.

Clone this wiki locally