SQL TOP, LIMIT, FETCH FIRST Clause (original) (raw)

Last Updated : 10 Nov, 2025

The SQL TOP, LIMIT, and FETCH FIRST clauses are used to restrict the number of rows returned by a query. They help in retrieving only a small portion of data from a large table, which makes queries faster and easier to read.

**SQL SELECT TOP Clause

The SELECT TOP clause in SQLonly returns the specified number of rows from the table. It is valuable on enormous tables with a large number of records. Returning countless records can affect execution.

The SQL TOP keyword is utilized with these database systems:

**Syntax:

SELECT column1, column2, ... TOP count FROM table_name [WHERE conditions] [ORDER BY expression [ ASC | DESC ]];

Let’s understand this using an example of SQL SELECT TOP statement. We will use the following table for this example:

New_emp

**Example 1: Using SELECT TOP Clause in SQL

In this example, we will fetch the top 4 rows from the table.

**Query:

SELECT TOP 4* FROM Employee;

**Output:

Sql-1

**In this query:

Example 2: SQL SELECT TOP with ORDER BY Clause

In this example, we will use the SQL SELECT TOP clause with ORDER BY clause to sort the data in the results set.

**Query

SELECT TOP 4* FROM Employee ORDER BY Salary DESC;

**Output:

Sql-2

**In this query:

Example 3: SQL SELECT TOP Clause with WHERE Clause Example

In this example, we will use the SELECT TOP clause with **WHERE clause to filter data on specific conditions.

**Query:

SELECT TOP 2* FROM Employee WHERE Salary>2000 ORDER BY Salary;

**Output:

sql-3

**In this query:

**Example 4: SQL SELECT TOP PERCENT Clause Example

The PERCENT keyword is utilized to select the primary and percent of all-out rows. For example,

**Query:

SELECT TOP 50 PERCENT* FROM Employee;

**Output:

sql select top percent clause example output

SQL SELECT TOP PERCENT Clause Example Output

**In this query:

Example 5: SQL TOP PERCENT with WHERE Clause Example

We can also include some situations using the TOP PERCENT with the WHERE clause in the above query.

**Query:

SELECT TOP 50 PERCENT* FROM Employee WHERE Salary<50000;

**Output:

sql-5

**In this query:

**SQL LIMIT **Clause

SQL LIMIT Clauselimits the number of results returned in the results set. The LIMIT Clause is utilized with the accompanying database systems:

Example 1: SELECT LIMIT Clause in SQL

In this example, we will use the SELECT LIMIT clause to display only 2 results.

**Query:

SELECT * FROM Employee WHERE Salary = 45000 LIMIT 2;

**Output:

Emp-1

**In this query:

Example 2: SQL LIMIT with WHERE Clause

The accompanying query selects the initial 4 records from the Employee table with a given condition.

**Query:

SELECT * FROM Employee WHERE Salary = 45000 LIMIT 2;

**Output:

Emp-1

**In this query:

Example 3: SQL LIMIT With OFFSET Clause

The OFFSET keyword is utilized to indicate beginning rows from where to select rows. For instance,

**Query:

SELECT * FROM Employee LIMIT 2 OFFSET 2;

**Output:

emp-2

**In this query:

SQL FETCH FIRST Clause

SQL FETCH FIRST clause fetches the first given number of rows from the table.

It is supported in database systems like:

**Syntax:

The syntax to use the FETCH FIRST clause in SQL is:

SELECT columns FROM table WHERE condition FETCH FIRST n ROWS ONLY;

Example 1: SQL FETCH FIRST Clause

In this example, we will fetch the first 3 rows from the table.

**Query:

SELECT * FROM Employee FETCH FIRST 3 ROWS ONLY;

**Output:

sql-10

**In this query:

Example 2: SQL FETCH FIRST PERCENT

In this example, we will fetch first 50% of the data from the table.

**Query:

SELECT * FROM Employee FETCH FIRST (SELECT CEIL(COUNT(*) / 2) FROM Employee) ROWS ONLY;

**Output:

sql-11

**In this query:

**Example 3: SQL FETCH FIRST with WHERE Clause

The "FETCH FIRST" syntax is not supported in MySQL. The correct syntax for limiting the number of rows in MySQL is by using the LIMIT clause.

**Query:

SELECT * FROM Employee WHERE Salary = 45000 FETCH FIRST 1 ROW ONLY;

**Output:

sql-12-

**In this query: