Implement MySQL LIMIT and OFFSET in a single query stating its difference


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)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jun-2020

560 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements