Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL ORDER BY (Sorting) Statement

MySQL ORDER BY (Sorting) Statement

We know that SELECT statements are used to read data from MySQL tables.

If we need to sort the read data, we can use MySQL's ORDER BY clause to set which field and which way you want to sort, and then return the search results.

MySQL ORDER BY (sorting) The statement can sort by one or more column values in ascending (ASC) or descending (DESC) order.

Syntax

The following is a SELECT statement using ORDER BY clause to sort the queried data before returning it:



SELECT column1, column2, ...

FROM table_name

ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;

Parameter Description:

More notes:

Example

Here are some examples of using the ORDER BY clause.

  1. Single-column sorting:
SELECT * FROM products

ORDER BY product_name ASC;

The above SQL statement selects all products from the products table and sorts them by product name in ascending (ASC) order.

  1. Multi-column sorting:
SELECT * FROM employees

ORDER BY department_id ASC, hire_date DESC;

The above SQL statement selects all employees from the employees table, first sorting by department ID in ascending (ASC) order, and then within the same department by hire date in descending (DESC) order.

  1. Using numbers to indicate column positions:
SELECT first_name, last_name, salary

FROM employees

ORDER BY 3 DESC, 1 ASC;

The above SQL statement selects the name and salary columns from the employees table, sorting by the third column (salary) in descending (DESC) order, then by the first column (first_name) in ascending (ASC) order.

  1. Sorting using expressions:


SELECT product_name, price * discount_rate AS discounted_price

FROM products

ORDER BY discounted_price DESC;

The above SQL statement selects the product name and the discounted price calculated from the discount rate in the products table, and sorts by the discounted price in descending (DESC) order.

5. Starting from MySQL 8.0.16, you can use NULLS FIRST or NULLS LAST to handle NULL values:



SELECT product_name, price

FROM products

ORDER BY price DESC NULLS LAST;

The above SQL statement selects the product name and price from the products table, sorting by price in descending (DESC) order, with NULL values placed last.

Conversely, if you want NULL values placed first, you can write:



SELECT product_name, price

FROM products

ORDER BY price DESC NULLS FIRST;

The ORDER BY clause is a powerful tool that can sort query results according to different business needs. In practical applications, pay attention to choosing appropriate columns and sort order to achieve the desired sorting effect.


Using the ORDER BY clause in the command prompt

The following uses the ORDER BY clause in a SELECT statement to read data from the MySQL table runoob_tbl:

Example

Try the following examples; the results will be sorted in ascending and descending order.

SQL sorting

mysql

use

RUNOOB

;

Database

changed

mysql

SELECT

from

runoob_tbl

ORDER

BY

submission_date

ASC

; +-----------+---------------+---------------+-----------------+ |

runoob_id

|

runoob_title

|

runoob_author

|

submission_date

| +-----------+---------------+---------------+-----------------+ |

3

| Learning

Java

|

RUNOOB

.

COM

|

2015

05

01

| |

4

| Learning

Python

|

RUNOOB

.

COM

|

2016

03

06

| |

1

| Learning

PHP

| Runoob Tutorial |

2017

04

12

| |

2

| Learning

MySQL

| Runoob Tutorial |

2017

04

12

| +-----------+---------------+---------------+-----------------+

4

rows

in

set

(

0.01

sec

)

mysql

SELECT

from

runoob_tbl

ORDER

BY

submission_date

DESC

; +-----------+---------------+---------------+-----------------+ |

runoob_id

|

runoob_title

|

runoob_author

|

submission_date

| +-----------+---------------+---------------+-----------------+ |

1

| Learning

PHP

| Runoob Tutorial |

2017

04

12

| |

2

| Learning

MySQL

| Runoob Tutorial |

2017

04

12

| |

4

| Learning

Python

|

RUNOOB

.

COM

|

2016

03

06

| |

3

| Learning

Java

|

RUNOOB

.

COM

|

2015

05

01

| +-----------+---------------+---------------+-----------------+

4

rows

in

set

(

0.01

sec

)

Read all data from the runoob_tbl table and sort it by the submission_date field in ascending order.


Using the ORDER BY clause in PHP scripts

You can use the PHP function mysqli_query() with the same SELECT command with an ORDER BY clause to retrieve data.

This function is used to execute SQL commands, and then the PHP function mysqli_fetch_array() is used to output all the queried data.

Example

Try the following example; the queried data is returned sorted by the submission_date field in descending order.

MySQL ORDER BY Test:

Runoob Tutorial MySQL ORDER BY Test

' ; echo ' ' ; while ( $row = mysqli_fetch_array ( $retval , MYSQLI_ASSOC ) ) { echo " " . " " . " " . " " . " " ; } echo '
Tutorial ID Title Author Submission Date
{$row['runoob_id']}{$row['runoob_title']}{$row['runoob_author']}{$row['submission_date']}
' ; mysqli_close ( $conn ) ; ?>

The output is shown in the figure below: