SQL SELECT TOP Get Certified
Learn how to limit the number of records returned by a SQL SELECT statement using SQLite-compatible LIMIT syntax.
What is SELECT TOP?
The TOP clause is used in SQL Server to limit the number of rows returned by a
SELECT statement. SQLite does not support the TOP clause.
Instead, SQLite uses the LIMIT clause to restrict the number of rows returned.
SQLite Equivalent of SELECT TOP
In SQL Server, you might write SELECT TOP 5. In SQLite, the equivalent is LIMIT 5 at the end of the query.
What this code does: Selects the name column from a table of ten employees and adds LIMIT 5 at the end, so only the first five rows are returned. Those are James Smith, Emily Johnson, Michael Brown, Olivia Davis and William Miller. Because there is no ORDER BY, these are simply the first five rows in the order the table provides them.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name
FROM employees
LIMIT 5;
Selecting the First 5 Records
The SQLite LIMIT clause can be used to return only the first five records from a table.
What this code does: Returns the name and salary of the first five employees in the table. The other five employees are skipped, so the result has exactly five rows. This is useful for a quick preview of a large table without loading every record.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, salary
FROM employees
LIMIT 5;
LIMIT with ORDER BY
To retrieve the highest or lowest values, combine ORDER BY with LIMIT. This is the SQLite equivalent of using TOP with ORDER BY in SQL Server.
What this code does: First sorts all ten employees from the highest salary to the lowest, and then keeps only the first five rows of that sorted list. The result is the five best-paid employees: Matthew Anderson (102000), James Smith (95000), Olivia Davis (88000), Emma Taylor (81000) and William Miller (72000). The sort happens before LIMIT is applied, which is why the order matters.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 5;
LIMIT with WHERE
You can combine WHERE, ORDER BY, and LIMIT to filter records and then return only a specific number of matching rows.
What this code does: Filters to employees earning more than 60000, sorts them from highest to lowest salary, and then allows up to 10 rows. Only seven employees earn more than 60000, so the query returns seven rows, from Matthew Anderson (102000) down to Emily Johnson (62000). LIMIT is a maximum, not a guarantee, so it never adds rows that do not exist.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, salary
FROM employees
WHERE salary > 60000
ORDER BY salary DESC
LIMIT 10;
LIMIT with Multiple Columns
You can select multiple columns and use LIMIT to restrict the number of rows returned.
What this code does: Shows the name, department and salary of the three highest-paid employees. Sorting by salary in descending order and limiting the result to three rows returns Matthew Anderson, James Smith and Olivia Davis, who all happen to work in Engineering.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, department, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;
LIMIT with DISTINCT
SQLite also allows DISTINCT to be combined with LIMIT to return a limited number of unique values.
What this code does: Removes duplicate departments with DISTINCT and then limits the result to five rows. The ten employees belong to only five different departments (Engineering, Marketing, Sales, Finance and HR), so all five unique names are returned. If there were more than five departments, the extra ones would be cut off by LIMIT.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT DISTINCT department
FROM employees
LIMIT 5;
LIMIT with OFFSET
SQLite supports OFFSET with LIMIT. This allows you to skip a specified number of rows before returning the requested number of records.
What this code does: Sorts employees by salary from highest to lowest, skips the first 5 rows with OFFSET 5, and then returns the next 5. The result is the second “page” of the ranking: Daniel Moore (66000), Emily Johnson (62000), Ava Thomas (59000), Michael Brown (58000) and Sophia Wilson (54000). This is the technique behind pagination, such as “page 2 of results”.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 5 OFFSET 5;
Finding the Highest-Paid Employees
By sorting salary in descending order and using LIMIT, you can retrieve the employees with the highest salaries.
What this code does: Sorts by salary from highest to lowest and keeps only the top three rows. The three highest-paid employees are Matthew Anderson (102000), James Smith (95000) and Olivia Davis (88000). Changing the number after LIMIT changes how many top earners are shown.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;
Finding the Lowest-Paid Employees
To find the employees with the lowest salaries, sort the salary column in ascending order and use LIMIT.
What this code does: Sorts by salary from lowest to highest with ASC and keeps the first three rows. The three lowest-paid employees are Sophia Wilson (54000), Michael Brown (58000) and Ava Thomas (59000). Reversing the sort direction is all it takes to switch from the top earners to the bottom earners.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, salary
FROM employees
ORDER BY salary ASC
LIMIT 3;
SQL Server TOP vs SQLite LIMIT
SQL Server and SQLite use different syntax for limiting query results.
SQL Server uses TOP, while SQLite uses LIMIT.
SELECT TOP 5LIMIT 5TOP 5 + ORDER BYORDER BY + LIMIT 5OFFSET / FETCHLIMIT + OFFSETWhy Use LIMIT in SQLite?
- Restricts the number of rows returned.
- Works directly with SQLite.
- Can be combined with
WHEREandORDER BY. - Supports pagination using
OFFSET. - Useful when working with large result sets.
Best Practices
- Use
ORDER BYwhen you need predictable results. - Use
WHEREto filter records before applyingLIMIT. - Use
LIMITinstead ofTOPwhen working with SQLite. - Use
OFFSETwhen implementing pagination. - Use
DISTINCTwithLIMITwhen you need unique values.
The SELECT TOP concept is used to limit query results. SQL Server uses
TOP, while SQLite uses LIMIT. Click Test My Code
on any example above to run it live, or Code Explanation to walk through
the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.