- 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
MySQL query to delete a record with the lowest ID?
To delete record with the lowest id, you can use the following syntax:
delete from yourTableName order by yourColumnName limit 1;
Let us first create a table:
mysql> create table DemoTable ( Id int, Name varchar(20) ); Query OK, 0 rows affected (0.75 sec)
Following is the query to insert records in the table using insert command:
mysql> insert into DemoTable values(10,'Larry'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(100,'Mike'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(30,'Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(90,'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(9,'Carol'); Query OK, 1 row affected (0.19 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output:
+------+-------+ | Id | Name | +------+-------+ | 10 | Larry | | 100 | Mike | | 30 | Sam | | 90 | Chris | | 9 | Carol | +------+-------+ 5 rows in set (0.00 sec)
Following is the query to delete record with lowest id:
mysql> delete from DemoTable order by Id limit 1; Query OK, 1 row affected (0.18 sec)
Let us display all records from the table to check the lowest id has been deleted or not:
mysql> select *from DemoTable;
This will produce the following output
+------+-------+ | Id | Name | +------+-------+ | 10 | Larry | | 100 | Mike | | 30 | Sam | | 90 | Chris | +------+-------+ 4 rows in set (0.00 sec)
- Related Articles
- Get the new record key ID from MySQL insert query?
- MySQL query to perform delete operation where id is the biggest?
- Delete a record with tablename.columnname= value in MySQL
- MySQL query to search record with apostrophe?
- MySQL query to select a record with two exact values?
- How to use three conditions in a single MySQL query with id, name and age of students to fetch record of a student?
- MySQL query to find the average of rows with the same ID
- MySQL query to delete row
- MySQL query to place a specific record on the top
- Display the record with non-duplicate Id using MySQL GROUP BY and HAVING
- MySQL query to fetch record by year
- How to return the nth record from MySQL query?
- Delete one row and reorder the others with the correct ID in MySQL?
- Delete a specific record on the basis of EmployeeId in MySQL
- MySQL query to decrease the value of a specific record to zero?