
- 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
Select last 3 rows from database order by id ASC?
You can use subquery. Following is the syntax −
SELECT * FROM ( SELECT * FROM yourTableName ORDER BY yourIdColumnName DESC LIMIT 3 ) anyAliasName ORDER BY yourIdColumnName;
Let us first create a table −
mysql> create table DemoTable ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(100) ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(ClientName) values('Larry'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(ClientName) values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(ClientName) values('Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(ClientName) values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(ClientName) values('Carol'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(ClientName) values('Robert'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(ClientName) values('Sam'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(ClientName) values('Mike'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 1 | Larry | | 2 | Chris | | 3 | Bob | | 4 | David | | 5 | Carol | | 6 | Robert | | 7 | Sam | | 8 | Mike | +----------+------------+ 8 rows in set (0.00 sec)
Following is the query to select last 3 rows from database order by id ASC −
mysql> SELECT * FROM ( SELECT * FROM DemoTable ORDER BY ClientId DESC LIMIT 3 ) tbl ORDER BY ClientId ASC;
This will produce the following output −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 6 | Robert | | 7 | Sam | | 8 | Mike | +----------+------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to select distinct order by id
- Order By date ASC in MySQL?
- How to select last 10 rows from MySQL?
- How to order last 5 records by ID in MySQL
- Order by last 3 chars in MySQL?
- Implement MySQL ORDER BY without using ASC or DESC?
- How to select the last three rows of a table in ascending order with MySQL?
- MySQL ORDER BY ASC and display NULLs at the bottom?
- MySQL - SELECT … WHERE id IN (..) order with particular column?
- Select two random rows in a MySQL database?
- How to select last two rows in MySQL?
- Order by last 3 months first, then alphabetically in MySQL?
- How to select all rows from a table except the last one in MySQL?
- In the query [SELECT column1, column2 FROM table_name WHERE condition; ] which clause among ‘SELECT’, ‘WHERE’ and ‘FROM’ is evaluated in the last by the database server and why?
- Select last 20 records ordered in ascending order in MySQL?

Advertisements