MySQL

MySQL ORDER BY

The ORDER BY keyword sorts the result set in ascending or descending order. To sort records in descending order, use DESC keyword. To sort in acending order, use ASC keyword.
 

Syntax:
SELECT column1, column2, ..., columnN
FROM tableX
ORDER BY column1, column2, ..., columnN ASC|DESC;

ASC is the default option.
 

 

Example 1: Sort in ascending order

The following statement selects all clients, sorted by age:

SELECT * FROM Clients ORDER BY Age;


 

Example 2: Sort in descending order

The following statement selects all clients, sorted in descending order by age:

SELECT * FROM Clients ORDER BY Age DESC;


 

 

Example 3: With several columns

The following statement selects all clients, sorted by “Age” and “Address” columns:

SELECT * FROM Clients ORDER BY Age, Adress;


 

Example 4: With several columns

The following statement selects all clients, sorted in ascending order by “Age” and in descending order by the “Address” column:

SELECT * FROM Clients ORDER BY Age ASC, Adress DESC;


mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *