- 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
How to select all rows from a table except the last one in MySQL?
You need to use != operator along with subquery. The syntax is as follows −
select *from yourTableName where yourIdColumnName != (select max(yourIdColumnName) from yourTableName );
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table AllRecordsExceptLastOne -> ( -> Id int NOT NULL AUTO_INCREMENT, -> UserName varchar(10), -> UserAge int -> , -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.65 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into AllRecordsExceptLastOne(UserName,UserAge) values('John',21); Query OK, 1 row affected (0.12 sec) mysql> insert into AllRecordsExceptLastOne(UserName,UserAge) values('Carol',28); Query OK, 1 row affected (0.18 sec) mysql> insert into AllRecordsExceptLastOne(UserName,UserAge) values('Mike',22); Query OK, 1 row affected (0.13 sec) mysql> insert into AllRecordsExceptLastOne(UserName,UserAge) values('Sam',29); Query OK, 1 row affected (0.14 sec) mysql> insert into AllRecordsExceptLastOne(UserName,UserAge) values('David',27); Query OK, 1 row affected (0.11 sec) mysql> insert into AllRecordsExceptLastOne(UserName,UserAge) values('Larry',24); Query OK, 1 row affected (0.20 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from AllRecordsExceptLastOne;
The following is the output −
+----+----------+---------+ | Id | UserName | UserAge | +----+----------+---------+ | 1 | John | 21 | | 2 | Carol | 28 | | 3 | Mike | 22 | | 4 | Sam | 29 | | 5 | David | 27 | | 6 | Larry | 24 | +----+----------+---------+ 6 rows in set (0.00 sec)
Here is the query to select all rows from a table except the last one −
mysql> select *from AllRecordsExceptLastOne where Id!=(select max(Id) from AllRecordsExceptLastOne);
The following is the output −
+----+----------+---------+ | Id | UserName | UserAge | +----+----------+---------+ | 1 | John | 21 | | 2 | Carol | 28 | | 3 | Mike | 22 | | 4 | Sam | 29 | | 5 | David | 27 | +----+----------+---------+ 5 rows in set (0.04 sec)
- Related Articles
- Select all rows except from today in MySQL?
- How to select last 10 rows from MySQL?
- How to select all the records except a row with certain id from a MySQL table?
- How to select the last three rows of a table in ascending order with MySQL?
- How to select last two rows in MySQL?
- How to delete all rows except some in MySQL?
- How to select all columns except one in a Pandas DataFrame?
- MySQL select query to select rows from a table that are not in another table?
- MySQL - How to count all rows per table in one query?
- How to select all the data from a table using MySQL in Python?
- Select rows from a MySQL table and display using IN()
- How to copy rows from one table to another in MySQL?
- How to select record from last 6 months in a news table using MySQL?
- How can we delete all rows from a MySQL table?
- Move rows from one table to another in MySQL?

Advertisements