Chrome Extension

Order by ascending and descending MySQL

SELECT * FROM table_name FROM ORDER BY column_name ASC
SELECT * FROM table_name FROM ORDER BY column_name DESC
We are a bunch of lazy programmers so we developed this chrome extension
search code from your favorite search engine
Install Now

Order by clause can be used to select MySQL Records in ascending and descending orders. If you want to order the rows in ascending order use below order by clause after select query.

ORDER BY column_name ASC

To sort the Rows in descending order use below after select query.

ORDER BY column_name DESC

Example

If there is students table and you want to get student records in ascending and descending order by Id. Use below queries

id first_name last_name
1 John Deo
2 Rick Grimes
3 Carol Dewene
4 Enith Brian

To get students in ascending order

SELECT * FROM students ORDER BY Id ASC

To get students in ascending order

SELECT * FROM students ORDER BY Id DESC

Order By Multiple Columns

You can also sort or order records by multiple columns. Below is the query.

SELECT
	first_name,
	last_name
FROM
	students
ORDER BY
	first_name DESC,
	last_name ASC;

Sort records using Order by Expression

You can use the expression in place of column names when using the ORDER BY clause. Below is the example

SELECT 
    column_name_1,
    column_name_1 * column_name_2
FROM
    table_name
ORDER BY 
   column_name_1 * column_name_1 ASC;

Sort records by column field values

 You can also sort or order records by its field values. For example, if there is a column named as FileProcessed and it has three repetitive values as 'success', 'error' and 'warning'. And you want to sort the records based on these values in an order of (error, warning, success). You can do that using the below query.

SELECT 
    file_name, 
    file_processed
FROM
    file_data
ORDER BY 
    FIELD(file_processed,
        'error',
        'warning',
        'success');

Search Index Data (The code snippet can also be found with below search text)

order by mysql
Was this helpful?
0 Comments
Open Code Manager We are hiring