🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In this chapter, we will learn about the LIMIT clause in MySQL. The LIMIT clause is used to specify the number of rows to return in the result set of a query. This is particularly useful for pagination, retrieving a subset of records, and improving performance when dealing with large datasets. We will cover the syntax, examples, and important considerations for using the LIMIT clause.
Syntax
The basic syntax for the LIMIT clause is:
SELECT column1, column2, ...
FROM table_name
LIMIT number_of_rows OFFSET offset;
number_of_rows: The maximum number of rows to return.offset: The number of rows to skip before starting to return rows (optional).
Using LIMIT
Example
SELECT first_name, last_name, email
FROM students
LIMIT 3;
This example retrieves the first 3 rows from the students table.
Using LIMIT with OFFSET
Example
SELECT first_name, last_name, email
FROM students
LIMIT 3 OFFSET 2;
This example skips the first 2 rows and retrieves the next 3 rows from the students table.
Combining LIMIT with ORDER BY
The LIMIT clause is often used in conjunction with the ORDER BY clause to sort the results before limiting the number of rows.
Example
SELECT first_name, last_name, email
FROM students
ORDER BY enrollment_date DESC
LIMIT 2;
This example sorts the results by enrollment_date in descending order and retrieves the first 2 rows.
Full Example
Let's go through a full example where we create a table, insert data into it, and use the LIMIT clause to retrieve specific subsets of data.
- Create a Database:
CREATE DATABASE school;
- Select the Database:
USE school;
- Create a Table:
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
enrollment_date DATE
);
- Insert Data into the Table:
INSERT INTO students (first_name, last_name, email, enrollment_date) VALUES
('Rahul', 'Sharma', 'rahul.sharma@example.com', '2023-07-01'),
('Priya', 'Singh', 'priya.singh@example.com', '2023-07-02'),
('Amit', 'Kumar', 'amit.kumar@example.com', '2023-07-03'),
('Neha', 'Verma', 'neha.verma@example.com', '2023-07-04'),
('Sahil', 'Mehta', 'sahil.mehta@example.com', '2023-07-05');
- Use the LIMIT Clause:
SELECT first_name, last_name, email
FROM students
LIMIT 3;
Output
| first_name | last_name | |
|---|---|---|
| Rahul | Sharma | rahul.sharma@example.com |
| Priya | Singh | priya.singh@example.com |
| Amit | Kumar | amit.kumar@example.com |
- Use the LIMIT Clause with OFFSET:
SELECT first_name, last_name, email
FROM students
LIMIT 3 OFFSET 2;
Output
| first_name | last_name | |
|---|---|---|
| Amit | Kumar | amit.kumar@example.com |
| Neha | Verma | neha.verma@example.com |
| Sahil | Mehta | sahil.mehta@example.com |
- Combine LIMIT with ORDER BY:
SELECT first_name, last_name, email
FROM students
ORDER BY enrollment_date DESC
LIMIT 2;
Output
| first_name | last_name | |
|---|---|---|
| Sahil | Mehta | sahil.mehta@example.com |
| Neha | Verma | neha.verma@example.com |
Important Considerations
- Performance: Using
LIMITcan improve query performance by reducing the number of rows returned, especially for large datasets. - Pagination: The
LIMITclause is commonly used for pagination in web applications, allowing you to retrieve a specific subset of results per page. - Combining with ORDER BY: To ensure consistent results when using
LIMIT, especially with pagination, always use it with anORDER BYclause to specify the sort order of the rows.
Conclusion
The LIMIT clause is used for controlling the number of rows returned by a query in MySQL. This chapter covered how to use the LIMIT clause, including examples of retrieving a specific number of rows, using an offset, and combining it with the ORDER BY clause.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment