- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fetch ordered records after a specific limit in MySQL
For this, you can use a subquery. Let us first create a table −
mysql> create table DemoTable618 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,StudentFirstName varchar(100) ); Query OK, 0 rows affected (1.45 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable618(StudentFirstName) values('David'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable618(StudentFirstName) values('Chris'); Query OK, 1 row affected (0.44 sec) mysql> insert into DemoTable618(StudentFirstName) values('Robert'); Query OK, 1 row affected (0.54 sec) mysql> insert into DemoTable618(StudentFirstName) values('Sam'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable618(StudentFirstName) values('Mike'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable618(StudentFirstName) values('Carol'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable618(StudentFirstName) values('Bob'); Query OK, 1 row affected (3.66 sec) mysql> insert into DemoTable618(StudentFirstName) values('John'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable618;
This will produce the following output −
+-----------+------------------+ | StudentId | StudentFirstName | +-----------+------------------+ | 1 | David | | 2 | Chris | | 3 | Robert | | 4 | Sam | | 5 | Mike | | 6 | Carol | | 7 | Bob | | 8 | John | +-----------+------------------+ 8 rows in set (0.00 sec)
Here is the query to fetch ordered records after a specific limit −
mysql> select *from (select StudentId,StudentFirstName from DemoTable618 where StudentId >= 5 order by StudentId limit 4) tbl order by tbl.StudentId desc;
This will produce the following output −
+-----------+------------------+ | StudentId | StudentFirstName | +-----------+------------------+ | 8 | John | | 7 | Bob | | 6 | Carol | | 5 | Mike | +-----------+------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Fetch a single ordered date from a column with MySQL LIMIT
- Fetch records containing a specific character twice in MySQL
- Order date records and fetch the 2nd ordered record in MySQL
- UNIX_TIMESTAMP with date in MySQL query to fetch records after a specific date in different format?
- How to order records and fetch some using MySQL LIMIT?
- MySQL to fetch records based on a specific month and year?
- How can fetch records from specific month and year in a MySQL table?
- MySQL RegExp to fetch records with only a specific number of words
- MySQL REGEXP to fetch string + number records beginning with specific numbers?
- MySQL query to fetch specific records matched from an array (comma separated values)
- Create a Stored Procedure with MySQL and set a limit to display only a specific number of records
- Fetch a specific column value (name) in MySQL
- Find records on or after a specific date in MongoDB?
- Select last 20 records ordered in ascending order in MySQL?
- MySQL db query to fetch records from comma separate values on the basis of a specific value

Advertisements