

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 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 Questions & Answers
- How to change auto increment number in MySQL?
- Passing NULL to MySQL for auto increment?
- How to make MySQL table primary key auto increment?
- Change the Auto Increment counter in MySQL?
- Set MySQL int column to auto increment by 1 beginning at 10000?
- How to get the next auto-increment id in MySQL?
- How to set initial value and auto increment in MySQL?
- How to change auto increment number in the beginning in MySQL?
- Set custom Auto Increment with ZEROFILL in MySQL
- How to handle fragmentation of auto increment ID column in MySQL?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
- How to auto-increment value of tables to lower value in MySQL?
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- How to create a table with auto-increment column in MySQL using JDBC?
- Two columns as primary key with auto-increment in MySQL?
- How do I begin auto increment from a specific point in MySQL?
Advertisements