SQL - LIMIT Clause



The SQL LIMIT clause

The SQL LIMIT clause is used to restrict the number of rows returned by the SELECT statement. In addition, we can also use it with UPDATE and DELETE statements to limit (restrict) the resultant records.

For instance, if you have a large number of rows stored in a database table, and you only want to get a certain number of rows for e.g. the first 10 rows, you can use the LIMIT clause in your query.

Microsoft SQL Server does not support the LIMIT clause. Instead of this, we can use the TOP clause to select a limited number of records from a table in MS SQL Server database. Similarly, Oracle supports the ROWNUM clause to restrict the records of a table.

Syntax

Following is the basic syntax of the SQL LIMIT clause with a SELECT statement −

SELECT column_name(s)
FROM table_name
WHERE [condition]
LIMIT value;

Where, value is the number of rows to return from the top.

Example

Assume we have created a table named CUSTOMERS which contains the personal details of customers including their name, age, address and salary etc. as shown below −

CREATE TABLE CUSTOMERS (
   ID INT NOT NULL,
   NAME VARCHAR (20) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (25),
   SALARY DECIMAL (18, 2),
   PRIMARY KEY (ID)
);

Now, insert values into this table using the INSERT statement as follows −

INSERT INTO CUSTOMERS VALUES
   (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),
   (2, 'Khilan', 25, 'Delhi', 1500.00 ),
   (3, 'Kaushik', 23, 'Kota', 2000.00 ),
   (4, 'Chaitali', 25, 'Mumbai', 6500.00 ),
   (5, 'Hardik', 27, 'Bhopal', 8500.00 ),
   (6, 'Komal', 22, 'Hyderabad', 4500.00 ),
   (7, 'Muffy', 24, 'Indore', 10000.00 );

The table will be created as follows −

ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 Hyderabad 4500.00
7 Muffy 24 Indore 10000.00

Now, we are using the LIMIT clause to fetch the top 3 records from the CUSTOMERS table without specifying any clauses (to filter the data) such as WHERE, ORDER BY, etc. −

SELECT * FROM CUSTOMERS LIMIT 3;

Output

This will produce the following result −

ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2000.00

LIMIT with ORDER BY Clause

The ORDER BY clause in SQL is used to sort the result-set of a query in ascending or descending order. We can use it along with the LIMIT clause in MySQL database to retrieve (or, update or, delete etc.) first N number of records in sorted order.

Example

Using the following query we are retrieving the first 4 records of the CUSTOMERS table in a sorted order. Here, we are sorting the table in ascending order based on the AGE column −

SELECT * FROM CUSTOMERS ORDER BY AGE ASC LIMIT 4;

Output

We obtain the result as follows −

ID NAME AGE ADDRESS SALARY
6 Komal 22 Hyderabad 4500.00
3 Kaushik 23 Kota 2000.00
7 Muffy 24 Indore 10000.00
2 Khilan 25 Delhi 1500.00

Note − The ORDER BY clause sorts the data in ascending order by default. So, if we need to sort the data in descending order in SQL, we must use DESC keyword.

LIMIT with WHERE Clause

We can use the LIMIT clause with the WHERE clause to limit the given number of rows and filter them based on the specified condition.

Example

following is the query retrieves the details of the first two customers from the CUSTOMERS table whose name starts with K

SELECT * FROM CUSTOMERS WHERE NAME LIKE 'k%' LIMIT 2;

Output

Following result is produced −

ID NAME AGE ADDRESS SALARY
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2000.00

LIMIT Clause with DELETE Statement

In MySQL database, the LIMIT clause can be used with the DELETE statement to delete a specific number of rows that meet the given criteria.

Example

In the following query, we are using the DELETE statement with LIMIT clause. Here, we are deleting the top 2 customers whose NAME starts with K

DELETE FROM CUSTOMERS WHERE NAME LIKE 'K%' LIMIT 2;

Output

We get the output as shown below −

Query OK, 2 rows affected (0.01 sec)

Verification

We can verify whether the changes are reflected in a table by retrieving its contents using the SELECT statement. Following is the query to display the records in the CUSTOMERS table −

SELECT * FROM CUSTOMERS;

The table is displayed as follows −

ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
7 Muffy 24 Indore 10000.00
Advertisements