
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement MySQL LIMIT and OFFSET in a Single Query
The LIMIT tells about how many records you want while OFFSET gives the records from the given position+1. Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (1.33 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.59 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.60 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+--------+ | Name | +--------+ | John | | Chris | | David | | Bob | | Sam | | Mike | | Carol | | Robert | +--------+ 8 rows in set (0.00 sec)
Following is the query to implement MySQL limit offset −
mysql> select *from DemoTable limit 4 offset 3;
Output
This will produce the following output −
+-------+ | Name | +-------+ | Bob | | Sam | | Mike | | Carol | +-------+ 4 rows in set (0.00 sec)
Advertisements