SQL LIMIT Examples
Let’s look at some examples of the LIMIT clause in SQL to understand it’s working:
We will use the sample table name “Student” and write some LIMIT queries.
To create this table in your system, write the following queries:
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
INSERT INTO student (id, name, age)
VALUES (1, 'Shubham Thakur', 18),
(2, 'Aman Chopra', 19),
(3, 'Bhavika uppala', 20),
(4,'Anshi Shrivastava',22);
LIMIT Clause Example
In this example, we will only retrieve 3 rows from the student table using LIMIT.
Query:
SELECT * FROM student
LIMIT 3;
Output:
LIMIT with ORDER BY Clause
In this example, we will use the LIMIT clause with ORDER BY clause
Query:
SELECT * FROM Student
ORDER BY Grade DESC
LIMIT 3;
Output:
The LIMIT operator can be used in situations such as the above, where we need to find the top 3 students in a class and do not want to use any conditional statements.
SQL LIMIT Clause
The LIMIT clause in SQL allows users to control the amount of data retrieved and displayed in the result set.
It is useful when only a subset of records is needed for analysis or display purposes in large databases with thousands of records.