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

Read Also:  What is the difference between a left join and a left outer join?

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.

Read Also:  Multidimensional array Difference in php

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Handle Multiple Checkbox Array
  2. Read Also:  Increment Decrement with Laravel 6 Example
  3. Read Also:  MySQL Remove special characters from database
  4. Read Also:  jQuery Ajax File Upload with Progress Bar
  5. Read Also:  Laravel 6 Update Increment column
  6. Read Also:  POST JSON Data via PHP curl Example
  7. Read Also:  Group By with Count using Laravel Collection
  8. Read Also:  Group By with Sum using Laravel Collection
  9. Read Also:  Group By Preserve Key using Laravel Collection
  10. Read Also:  order by multiple columns(fields) laravel query builder
CLOSEX