Python SQLite - LIMIT Clause

Last Updated : 4 Nov, 2026

SQLite LIMIT clause specifies the maximum number of rows a query can return. It can be combined with WHERE to filter records and ORDER BY to sort results before limiting them.

Syntax

SELECT column1, column2, ...
FROM table_name
[WHERE condition]
[ORDER BY column_name ASC | DESC]
LIMIT row_count [OFFSET offset_value];

  • column1, column2: Columns to retrieve.
  • WHERE: Filters records based on a condition.
  • ORDER BY: Sorts the result set before applying LIMIT.
  • row_count: Maximum number of rows to return.
  • OFFSET: Number of rows to skip before returning results.

Creating Table

Before using the LIMIT clause, let's create an employees table and insert sample records into it.

Python
import sqlite3

conn = sqlite3.connect(":memory:")
cursor = conn.cursor()

cursor.execute("""
CREATE TABLE employees (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    department TEXT NOT NULL,
    salary INTEGER NOT NULL
)
""")

cursor.executemany("""
INSERT INTO employees (id, name, department, salary)
VALUES (?, ?, ?, ?)
""", [
    (1, "Aditi", "HR", 42000),
    (2, "Rahul", "IT", 65000),
    (3, "Neha", "IT", 72000),
    (4, "Karan", "Sales", 38000),
    (5, "Priya", "Finance", 58000),
    (6, "Aman", "IT", 61000)
])

cursor.execute("SELECT * FROM employees")

print("ID  Name    Department  Salary")
print("-" * 35)

for row in cursor.fetchall():
    print(f"{row[0]:<3} {row[1]:<7} {row[2]:<12} {row[3]}")

conn.close()

Output

Screenshot_2026-10-26_11-40-48
Output

1. Using Basic LIMIT Clause

Following query retrieves the first three records when sorted by employee ID.

Python
cursor.execute("""
SELECT id, name, salary
FROM employees
ORDER BY id
LIMIT 3;
""")

print("id  name    salary")
for row in cursor.fetchall():
    print(f"{row[0]:<3} {row[1]:<7} {row[2]}")

Output

Screenshot_2026-10-26_11-50-14
Output

Explanation:

  • cursor.execute() executes the SQL query against the SQLite database.
  • ORDER BY id arranges records by employee ID in ascending order.
  • LIMIT 3 restricts the result to three rows.
  • fetchall() retrieves all rows returned by the query.

2. Using with Multiple Columns

It applies to rows, regardless of how many columns are selected. We can retrieve multiple columns while restricting the number of records.

Python
cursor.execute("""
SELECT name, department
FROM employees
ORDER BY id
LIMIT 4;
""")

print("name    department")
for row in cursor.fetchall():
    print(f"{row[0]:<7} {row[1]}")

Output

Screenshot_2026-10-26_11-52-09
Output

Explanation:

  • SELECT name, department retrieves two columns.
  • LIMIT 4 returns at most four records.
  • ORDER BY id ensures a predictable order for the selected records.

3. Using with WHERE and ORDER BY

It can be combined with WHERE to filter records and ORDER BY to sort matching results. This is useful when retrieving the highest-paid employees or other top-ranked records.

Python
cursor.execute("""
SELECT name, salary
FROM employees
WHERE salary >= 50000
ORDER BY salary DESC
LIMIT 3;
""")

print("name    salary")
for row in cursor.fetchall():
    print(f"{row[0]:<7} {row[1]}")

Output

Screenshot_2026-10-26_11-54-35
Output

Explanation:

  • WHERE salary >= 50000 selects employees earning at least 50,000.
  • ORDER BY salary DESC sorts salaries from highest to lowest.
  • LIMIT 3 returns only the top three matching employees.

4. Using with OFFSET

It skips a specified number of rows before returning results. Combining LIMIT and OFFSET allows us to retrieve different portions of a result set, which is commonly used for pagination.

Python
cursor.execute("""
SELECT id, name, department
FROM employees
ORDER BY id
LIMIT 2 OFFSET 2;
""")

print("id  name    department")
for row in cursor.fetchall():
    print(f"{row[0]:<3} {row[1]:<7} {row[2]}")

Output

Screenshot_2026-10-26_11-56-26
Output

Explanation:

  • cursor.execute() executes the query with LIMIT and OFFSET.
  • OFFSET 2 skips the first two rows.
  • LIMIT 2 returns the next two rows.
  • fetchall() retrieves the resulting records.

5. Using with Comma-Separated Values

It also supports a comma-separated form of LIMIT and OFFSET. In this syntax, the first number specifies the offset, and the second specifies the maximum number of rows.

Python
cursor.execute("""
SELECT id, name
FROM employees
ORDER BY id
LIMIT 2, 3;
""")

print("id  name")
for row in cursor.fetchall():
    print(f"{row[0]:<3} {row[1]}")

Output

Screenshot_2026-10-26_11-58-24
Output

Explanation:

  • First value, 2, skips two rows.
  • Second value, 3, returns up to three rows after skipping.
  • Form reverses the order of the arguments compared with LIMIT row_count OFFSET offset_value.
Comment