MySQL consists of various clauses for performing efficient data retrieval, and the LIMIT clause is essential for controlling the number of rows returned in a query. By specifying a limit on the number of results, the LIMIT clause helps manage large datasets, optimize performance, and focus on relevant data. This article will explore the LIMIT clause, showing its usage in terms of examples.
MySQL LIMIT Clause
The LIMIT clause in MySQL is used to specify the maximum number of rows returned by a query. It is particularly useful for managing large result sets and optimizing query performance. By setting a row limit, you can control the amount of data retrieved and efficiently paginate through results.
Syntax:
The syntax for using LIMIT Clause in MySQL is as follows:
SELECT column_name(s)
FROM table_name
LIMIT [offset,] row_count;
Parameters:
- offset (optional): The starting point from which rows are returned. If omitted, it defaults to 0.
- row_count: The maximum number of rows to return.
MySQL LIMIT Clause Example
Let’s look at some examples of the LIMIT Clause in MySQL. Learning the LIMIT Clause with examples will help in understanding the concept better.
First, let’s create a table:
Demo MySQL Database
CREATE TABLE employees (
employee_id INT,
name VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees (employee_id, name, salary) VALUES
(1, 'Gaurav', 75000.00),
(2, 'Yuvraj', 82000.00),
(3, 'Shruti', 69000.00),
(4, 'Anjali', 90000.00),
(5, 'Vaishnavi', 85000.00),
(6, 'Gauri', 78000.00);
SELECT * FROM employees;
Output:
+-------------+-----------+----------+
| employee_id | name | salary |
+-------------+-----------+----------+
| 1 | Gaurav | 75000.00 |
| 2 | Yuvraj | 82000.00 |
| 3 | Shruti | 69000.00 |
| 4 | Anjali | 90000.00 |
| 5 | Vaishnavi | 85000.00 |
| 6 | Gauri | 78000.00 |
+-------------+-----------+----------+
Example 1: Retrieve the First 3 Records
In this example, we are using the LIMIT clause to fetch the first 3 records from the employees table. This query returns the earliest entries based on their order in the table.
SELECT * FROM employees
LIMIT 3;
Output:
+-------------+--------+----------+
| employee_id | name | salary |
+-------------+--------+----------+
| 1 | Gaurav | 75000.00 |
| 2 | Yuvraj | 82000.00 |
| 3 | Shruti | 69000.00 |
+-------------+--------+----------+
Example 2: Retrieve 3 Records Starting from the 2nd Record
In this example, we are using the LIMIT clause with an offset to skip the first record and return the next 3 records from the employees table. This allows us to paginate through the results by specifying a starting point.
SELECT * FROM employees
LIMIT 1, 3;
Output:
+-------------+--------+----------+
| employee_id | name | salary |
+-------------+--------+----------+
| 2 | Yuvraj | 82000.00 |
| 3 | Shruti | 69000.00 |
| 4 | Anjali | 90000.00 |
+-------------+--------+----------+
Example 3: Retrieve the Last 2 Records
In this example, we are using the LIMIT clause in combination with ORDER BY to fetch the last 2 records from the employees table. Sorting the results in descending order and limiting the output lets us obtain the most recent entries.
SELECT * FROM employees
ORDER BY employee_id DESC
LIMIT 2;
Output:
+-------------+-----------+----------+
| employee_id | name | salary |
+-------------+-----------+----------+
| 6 | Gauri | 78000.00 |
| 5 | Vaishnavi | 85000.00 |
+-------------+-----------+----------+
Conclusion
In conclusion, the LIMIT clause in MySQL is a useful clause for controlling the number of rows returned by a query. It enables efficient data management by allowing you to focus on a subset of records, optimize performance, and paginate results. Understanding and using LIMIT can significantly enhance your ability to handle large datasets effectively.
Similar Reads
SQL LIMIT Clause
The LIMIT clause in SQL is used to control the number of rows returned in a query result. It is particularly useful when working with large datasets, allowing us to retrieve only the required number of rows for analysis or display. Whether we're looking to paginate results, find top records, or just
5 min read
SQLite Limit clause
SQLite is the most popular and most used database engine which is written in c programming language. It is serverless and self-contained and used for developing embedded software devices like TVs, Mobile Phones, etc. In this article, we will be learning about the SQLite Limit Clause using examples s
5 min read
PL/SQL LIMIT Clause
The LIMIT clause in PL/SQL is a powerful tool designed to manage the amount of data retrieved from a query, making it particularly useful for handling large datasets. By specifying the maximum number of rows to be fetched, the LIMIT clause helps in optimizing both performance and memory usage. In th
6 min read
MySQL DISTINCT Clause
When working with databases, you often need to filter out duplicate records to get a clear and accurate result set. MySQL offers a straightforward solution for this with the DISTINCT clause. This clause helps you retrieve only unique rows from your query results, making it an essential tool for data
4 min read
Python SQLite - LIMIT Clause
In this article, we are going to discuss the LIMIT clause in SQLite using Python. But first, let's get a brief about the LIMIT clause. If there are many tuples satisfying the query conditions, it might be resourceful to view only a handful of them at a time. LIMIT keyword is used to limit the data g
2 min read
MySQL HAVING Clause
In MySQL, the HAVING clause is used in conjunction with the GROUP BY clause to filter the results of a query based on aggregate functions. It provides a way to apply conditions to the grouped results, which cannot be achieved using the WHERE clause alone.The HAVING clause is essential when you need
3 min read
SQL Clauses
Structured Query Language (SQL) is a powerful language used to manage and manipulate relational databases. One of the essential features of SQL is its clauses, which allow users to filter, sort, group, and limit data efficiently. SQL clauses simplify querying and enhance database performance by retr
7 min read
Python PostgreSQL - Limit Clause
In this article, we are going to see how to use the limit clause in PostgreSQL using pyscopg2 module in Python. In PostgreSQL LIMIT constraints the number of rows returned by the query. By default, It is used to display some specific number of rows from the top. If we want to skip a number of rows b
2 min read
MySQL WHERE Clause
The MySQL WHERE clause is essential for filtering data based on specified conditions and returning it in the result set. It is commonly used in SELECT, INSERT, UPDATE, and DELETE statements to work on specific data. This clause follows the FROM clause in a SELECT statement and precedes any ORDER BY
5 min read
PL/SQL WITH Clause
The PL/SQL WITH clause is a powerful feature that enhances the readability and performance of your SQL queries. It allows you to define temporary result sets, which can be referenced multiple times within a single query. This feature is particularly useful for simplifying complex queries and improvi
5 min read