
- 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
Getting the last 30 rows in MySQL
To get the last 30 rows in MySQL, you need to use ORDER BY DESC and then LIMIT 30. The syntax is as follows −
select * from yourTableName order by yourColumnName DESC LIMIT 30;
Let us first create a table −
mysql> create table DemoTable1567 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY -> ); Query OK, 0 rows affected (0.82 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1567 values(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(); Query OK, 37 rows affected (0.37 sec) Records: 37 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select * from DemoTable1567;
This will produce the following output −
+----+ | Id | +----+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | | 11 | | 12 | | 13 | | 14 | | 15 | | 16 | | 17 | | 18 | | 19 | | 20 | | 21 | | 22 | | 23 | | 24 | | 25 | | 26 | | 27 | | 28 | | 29 | | 30 | | 31 | | 32 | | 33 | | 34 | | 35 | | 36 | | 37 | +----+ 37 rows in set (0.00 sec)
Following is the query to get last 30 rows −
mysql> select * from DemoTable1567 order by Id DESC LIMIT 30;
This will produce the following output −
+----+ | Id | +----+ | 37 | | 36 | | 35 | | 34 | | 33 | | 32 | | 31 | | 30 | | 29 | | 28 | | 27 | | 26 | | 25 | | 24 | | 23 | | 22 | | 21 | | 20 | | 19 | | 18 | | 17 | | 16 | | 15 | | 14 | | 13 | | 12 | | 11 | | 10 | | 9 | | 8 | +----+ 30 rows in set (0.00 sec)
- Related Articles
- Getting last value in MySQL group concat?
- Fetching rows added in last hour with MySQL?
- How to select last two rows in MySQL?
- MySQL query to find the number of rows in the last query
- How to select last 10 rows from MySQL?
- Getting last 5 character of a string with MySQL query?
- Get all rows apart from first and last in MySQL
- Select only 5 random rows in the last 50 entries With MySQL?
- Query MySQL table and fetch rows posted before the last 3 days?
- MySQL query to delete all rows older than 30 days?
- How to select all rows from a table except the last one in MySQL?
- How to select the last three rows of a table in ascending order with MySQL?
- Getting count of two different sets of rows in a table and then dividing them in MySQL
- Getting number of rows in table in an itab in SAP
- Getting the first part from the Postcode in MySQL

Advertisements