
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to order last 5 records by ID in MySQL
You can use subquery for this. Let us first create a table −
mysql> create table DemoTable ( Id int ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(115); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(200); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(140); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(124); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(198); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(156); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(178); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(156); Query OK, 1 row affected (0.43 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+ | Id | +------+ | 100 | | 115 | | 200 | | 140 | | 124 | | 198 | | 156 | | 178 | | 156 | +------+ 9 rows in set (0.00 sec)
Here is the query to get last 5 records ordered by ID −
mysql> select *from(select *from DemoTable order by Id DESC limit 5)tbl order by Id;
This will produce the following output −
+------+ | Id | +------+ | 156 | | 156 | | 178 | | 198 | | 200 | +------+ 5 rows in set (0.00 sec)
- Related Articles
- MySQL query to return 5 random records from last 20 records?
- Select last 20 records ordered in ascending order in MySQL?
- MySQL query to select distinct order by id
- How to ORDER BY last 2 character string in MySQL?
- Select last 3 rows from database order by id ASC?
- Implement ORDER BY in MySQL to order records in human readable format?
- Order by last 3 chars in MySQL?
- How to use ORDER BY field and sort by id in a single MySQL field?
- How to order records by a column in MySQL and place empty records at the end?
- How to ORDER BY DESC and display the first 3 records in MySQL?
- ORDER BY records in MySQL based on a condition
- How to specify exact order with WHERE `id` IN (…) in MySql?
- MySql how to display the records with latest ID in a table?
- Order by last 3 months first, then alphabetically in MySQL?
- How to limit records to only the last five results in MySQL

Advertisements