
- 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
Order records and delete n rows in MySQL
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstName varchar(20) -> ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Adam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName) values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(FirstName) values('Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(FirstName) values('Sam'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Adam | | 3 | John | | 4 | David | | 5 | Mike | | 6 | Carol | | 7 | Sam | +----+-----------+ 7 rows in set (0.00 sec)
Here is the query to order records and delete n rows −
mysql> delete from DemoTable order by FirstName asc limit 4; Query OK, 4 rows affected (0.19 sec)
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 3 | John | | 5 | Mike | | 7 | Sam | +----+-----------+ 3 rows in set (0.00 sec)
- Related Articles
- Order MySQL records randomly and display name in Ascending order
- Delete selective multiple records using MySQL DELETE query
- Order VARCHAR records with string and numbers in MySQL
- Delete all records from a table in MySQL?
- Remove specific fields/ rows and show other records in MySQL?
- How do I delete blank rows in MySQL?
- Get records in a certain order using MySQL?
- Delete all the records from a MySQL table?
- Implement ORDER BY in MySQL to order records in human readable format?
- Order date records and fetch the 2nd ordered record in MySQL
- Delete records from a MySQL table by excluding non-deleted records using NOT IN
- How to delete all rows except some in MySQL?
- Delete only specific rows in a table with MySQL
- Delete records where timestamp older than 5 minutes in MySQL?
- How to order records by a column in MySQL and place empty records at the end?

Advertisements