- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 DESC and display the first 3 records in MySQL?
For this, you can use ORDER BY DESC with LIMIT. Let us first create a table −
mysql> create table DemoTable ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserName varchar(100) ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(UserName) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(UserName) values('Robert'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(UserName) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(UserName) values('David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(UserName) values('Mike'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+----------+ | UserId | UserName | +--------+----------+ | 1 | Chris | | 2 | Robert | | 3 | Bob | | 4 | David | | 5 | Mike | +--------+----------+ 5 rows in set (0.00 sec)
Following is the query to display first 3 records after applying for ORDER BY DESC on a MySQL column −
mysql> select *from DemoTable order by UserId DESC LIMIT 0,3;
This will produce the following output −
+--------+----------+ | UserId | UserName | +--------+----------+ | 5 | Mike | | 4 | David | | 3 | Bob | +--------+----------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to display records ordered by DESC while skipping some?
- Wrap around to first value and implement MySQL ORDER BY ASC and DESC in a single query
- MySQL order by 0 first and then display the record in descending order?
- How to order DESC by a field, but list the NULL values first?
- Resolve Syntax error near “ORDER BY order DESC” in MySQL?
- Order MySQL records randomly and display name in Ascending order
- Order by a single field and display rest of the records in the same order with MySQL
- Order by desc except a single value in MySQL
- Display records by first fixing the first two values in a column and then using DISTINCT to display other values in MySQL
- Implement MySQL ORDER BY without using ASC or DESC?
- How to order records by a column in MySQL and place empty records at the end?
- How to order last 5 records by ID in MySQL
- Fix a row value and then ORDER BY DESC rest of the values in MySQL
- How to display first day and last day of the month from date records in MySQL?
- How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?

Advertisements