MySQL query to display records ordered by DESC while skipping some?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
(
   Id int,
   Name varchar(100)
);
Query OK, 0 rows affected (0.94 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10,'Chris');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(1,'David');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(4,'Bob');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(6,'Sam');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(7,'Mike');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(5,'Carol');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values(9,'John');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------+-------+
| Id   | Name |
+------+-------+
|   10 | Chris |
|    1 | David |
|    4 | Bob   |
|    6 | Sam   |
|    7 | Mike  |
|    5 | Carol |
|    9 | John  |
+------+-------+
7 rows in set (0.00 sec)

Following is the query to display records in DESC while skipping some. Here, we are displaying 4 records while skipping 3 −

mysql> select Name from DemoTable order by Id DESC limit 3,4;

This will produce the following output −

+-------+
| Name  |
+-------+
| Sam   |
| Carol |
| Bob   |
| David |
+-------+
4 rows in set (0.00 sec)
raja
Published on 27-Sep-2019 07:13:06
Advertisements