MySQL ORDER BY DESC & ASC

mysql order by two columns, DESC & ASC, limit, field, specific sequence, desc limit, alphabetical, date, case.

Syntax

SELECT 
   select_list
FROM 
   table_name
ORDER BY 
   column1 [ASC|DESC], 
   column2 [ASC|DESC],
   ...;

ORDER BY ascending & descending Order

ORDER BY column1 ASC;
ORDER BY column1 DESC;

clauses are equivalent:


ORDER BY column1 ASC;

and

ORDER BY column1;

MySQL ORDER BY clause to sort values in single column

SELECT
	relativemembersirname,
	relativemembername
FROM
	members
ORDER BY
	relativemembersirname;

relative member sirname in the descending order

SELECT
	relativemembersirname,
	relativemembername
FROM
	members
ORDER BY
	relativemembersirname DESC;

MySQL ORDER BY clause with multiple columns example

SELECT
	relativemembersirname,
	relativemembername
FROM
	members
ORDER BY
	relativemembersirname DESC,
	relativemembername ASC;

MySQL ORDER BY with an expression example

SELECT 
    stockNumber, 
    stocklinenumber, 
    qtyOrdered * amountEach
FROM
    stockdetails
ORDER BY 
   qtyOrdered * amountEach DESC;

SELECT clause a column alias

SELECT 
    stockNumber,
    stockLineNumber,
    qtyOrdered * amountEach AS subtotal
FROM
    stockdetails
ORDER BY subtotal DESC;

ORDER BY DESC & ASC

ORDER BY to sort data using a custom list

SELECT 
    stockNumber, 
    is_check
FROM
    stocks
ORDER BY 
    FIELD(is_check,
        'In Process',
        'On Hold',
        'Cancelled',
        'Resolved',
        'Disputed',
        'Shipped');

I hope you get an idea about ORDER BY.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment