
- 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
How to order by auto_increment in MySQL?
Let us first create a table −
mysql> create table DemoTable(Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,FirstName varchar(100)); Query OK, 0 rows affected (0.70 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FirstName) values('David'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(FirstName) values('Adam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName) values('Carol'); Query OK, 1 row affected (0.12 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 | Robert | | 3 | David | | 4 | Mike | | 5 | Adam | | 6 Carol | +----+-----------+ 6 rows in set (0.00 sec)
Following is the query to order by auto_increment −
mysql> select *from DemoTable order by Id DESC;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 6 | Carol | | 5 | Adam | | 4 | Mike | | 3 | David | | 2 | Robert | | 1 | Chris | +----+-----------+ 6 rows in set (0.00 sec)
- Related Articles
- How to ORDER BY RELEVANCE in MySQL?
- How to ORDER BY LIKE in MySQL?
- How to order by timestamp in MySQL?
- AUTO_INCREMENT in MySQL can be signed by default?
- How to ORDER BY grouped fields in MySQL?
- Reset AUTO_INCREMENT in MySQL
- How to sort by value with MySQL ORDER BY?
- How to order by date and time in MySQL?
- How to Order by a specific string in MySQL?
- How to order by a custom rule like order like 4,2,1,3 in MySQL?
- How to order MySQL rows by multiple columns?
- How to find if a column is auto_increment in MySQL?
- How to insert own values into auto_increment column in MySQL?
- How to change a primary key in MySQL to auto_increment?\n
- How to ORDER BY last 2 character string in MySQL?

Advertisements