
- 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 entry without using LIMIT in MySQL?
For this, you can use subquery. Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name) values('John Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Name) values('David Miller'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name) values('Chris Brown'); Query OK, 1 row affected (0.22 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+--------------+ | Id | Name | +----+--------------+ | 1 | John Smith | | 2 | David Miller | | 3 | Chris Brown | +----+--------------+ 3 rows in set (0.00 sec)
Here is the query to get the last entry without using LIMIT in MySQL −
mysql> select *from DemoTable -> where Id=(select max(Id) from DemoTable);
Output
+----+-------------+ | Id | Name | +----+-------------+ | 3 | Chris Brown | +----+-------------+ 1 row in set (0.00 sec)
- Related Articles
- How to select first and last row record using LIMIT in MySQL?
- Can we select second largest record from a table without using LIMIT clause in MySQL?
- Get Last Entry in a MySQL table?
- How to select the top two values using LIMIT in MySQL?
- Updating last entry of a particular ID in MySQL
- MySQL LIMIT to select a single row
- Check if table exist without using “select from” in MySQL?
- MySQL SELECT last few days?
- Limit length of longtext field in MySQL SELECT results?
- How to select last row in MySQL?
- Select text after last slash in MySQL?
- Why is “LIMIT 0” even allowed in MySQL SELECT statements?
- Select last day of current year in MySQL?
- How to select last two rows in MySQL?
- Select last record and update it in MySQL?

Advertisements