
- 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
Get a fixed number of results in descending order using a MySQL query
For descending order result, use DESC. However, LIMIT is used to get fixed number of records −
select *from yourTableName order by yourColumnName DESC LIMIT yourLimitNumber;
Let us first create a table −
mysql> create table DemoTable (Id int, Name varchar(100)); Query OK, 0 rows affected (0.73 sec)
Example
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(101,'Chris'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(102,'Robert'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(103,'Bob'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(104,'Adam'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(105,'James'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+------+--------+ | Id | Name | +------+--------+ | 100 | John | | 101 | Chris | | 102 | Robert | | 103 | Bob | | 104 | Adam | | 105 | James | +------+--------+ 6 rows in set (0.00 sec)
Get fixed number of records in descending order −
mysql> select *from DemoTable order by Name DESC LIMIT 3;
Output
+------+--------+ | Id | Name | +------+--------+ | 102 | Robert | | 100 | John | | 105 | James | +------+--------+ 3 rows in set (0.00 sec)
- Related Articles
- How to order results of a query randomly & select random rows in MySQL?
- MySQL query to get the count of values and display the count in a new column ordered in descending order\n
- MySQL query to select rows except first row in descending order?
- Populating a table from query results in MySQL?
- How to get number of rows in a table without using count(*) MySQL query?
- Get records in a certain order using MySQL?
- MySQL query to first set negative value in descending order and then positive value in ascending order
- Get all results using WHERE clause in MySQL?
- MySQL query to order by the first number in a set of numbers?
- Order MySQL results without identifier?
- Sort a column in descending order after placing argument in MySQL IN()?
- Can we return query results in same order as the values in MySQL `IN(…)` statement?
- MySQL query to order timestamp in descending order but place the timestamp 0000-00-00 00:00:00 first?
- MySQL query to display ASC order in number column?
- How to sort a Vector in descending order using STL in C++?

Advertisements