MySQL Order By

 

The ORDER BY clause is used to sort the result set which is not sorted by default. You can sort the result set by one or multiple columns and either in the ascending or descending order.

SELECT 
   column_name1, column_name2,...
FROM 
   table_name
ORDER BY 
    column_name1 [ASC|DESC], column_name2 [ASC|DESC],...

ASC – sort in ascending order
DESC – sort in descending order
* by default if [ASC|DESC] is omitted then ASC is used.

As you can see, our query is sorted by vender and ASC | DESC was omitted but MySQL sorted the result set in the ascending order(ASC).

ORDER BY with ASC and DESC

 

Here our query is sorted by vendor in ASC and price in DESC.

ORDER BY with custom order field

Using the FIELD() function you can sort the result set by column values.

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published.