English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to sort the data returned by a SELECT SQL query.
By default, when you use the SELECT statement to retrieve data from a table, the rows in the result set have no specific order. If you want to sort the result set in a specific order, you can specify the ORDER BY clause at the end of the statement, which tells the program how to sort the data returned by the query. The default sorting order is ascending.
ORDER BY clause is used to sort the data returned by the query in ascending or descending order. The basic syntax of this clause can be given as follows:
SELECT column_list FROM table_name ORDER BY column_name ASC|DESC;
Here,column_listthe database table to retrieve its valuename,age,countryand other columns/fieldnamewhilecolumn_nameThis is the name of the column to be sorted. Let's look at some examples to demonstrate how it actually works.
Consider that we have aemployeestable that has the following records:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 | | 2 | Tony Montana | 2002-07-15 | 6500 | 1 | | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | +--------+--------------+------------+--------+---------+
The following SQL statement will select fromemployeestable returns all employees and sorts them byemp_nameto sort the result set in ascending order by the
SELECT * FROM employees ORDER BY emp_name ASC;
You can omit the ASC option and use the following syntax. It returns the same result set as the previous statement because SQL defaults to ascending order:
SELECT * FROM employees ORDER BY emp_name;
After executing the above command, you will get the following output:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 2 | Tony Montana | 2002-07-15 | 6500 | 1 | +--------+--------------+------------+--------+---------+
Similarly, you can use the DESC option to sort in descending order. The following statement will sort the results in numericalSalaryresult set sorted in descending order by the (salary) column.
SELECT * FROM employees ORDER BY salary DESC;
This time, you will get the following result set:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | | 2 | Tony Montana | 2002-07-15 | 6500 | 1 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | | 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 | +--------+--------------+------------+--------+---------+
You can also specify multiple columns when sorting. However, unless there are some duplicate values in the table, the changes to the result set will not be visible. Alright, let's find out:
To better understand multi-column sorting, let's assume we have a table namedTraineestable containing the following records:
+----+------------+------------+-------------+--------+ | id | first_name | last_name | birth_date | gender | +----+------------+------------+-------------+--------+ | 1 | Peter | Parker | 1998-03-04 | M | | 2 | Harry | Potter | 2001-08-30 | M | | 3 | Peter | Pan | 2004-09-19 | M | | 4 | Alice | Kingsleigh | 1999-07-02 | F | | 5 | John | Connor | 2002-01-15 | M | +----+------------+------------+-------------+--------+
If you look at the table carefully, you will find that we have some duplicate values. However, the full names of trainees Peter Parker and Peter Pan are different, but their names are the same.
Now, execute the following command, which will sort the results byfirst_nameto sort the result set bySort.
SELECT * FROM trainees ORDER BY first_name;
After executing, you will get the following output:
+----+------------+------------+-------------+--------+ | id | first_name | last_name | birth_date | gender | +----+------------+------------+-------------+--------+ | 4 | Alice | Kingsleigh | 1999-07-02 | F | | 2 | Harry | Potter | 2001-08-30 | M | | 5 | John | Connor | 2002-01-15 | M | | 1 | Peter | Parker | 1998-03-04 | M | | 3 | Peter | Pan | 2004-09-19 | M | +----+------------+------------+-------------+--------+
Now, when you execute this statement, it will sort the results byfirst_nameandlast_nameto sort the result set bySort.
SELECT * FROM trainees ORDER BY first_name, last_name;
+----+------------+------------+-------------+--------+ | id | first_name | last_name | birth_date | gender | +----+------------+------------+-------------+--------+ | 4 | Alice | Kingsleigh | 1999-07-02 | F | | 2 | Harry | Potter | 2001-08-30 | M | | 5 | John | Connor | 2002-01-15 | M | | 3 | Peter | Pan | 2004-09-19 | M | | 1 | Peter | Parker | 1998-03-04 | M | +----+------------+------------+-------------+--------+
Did you notice the difference between the previous result set and the current result set-This time, the record of the trainee "Peter Parker" follows immediately after "Peter Pan".
Since both trainees' names are "Peter", in the records of these two traineeslast_nameThe column performs secondary sorting, which is why the record of the trained person "Peter Parker" comes after "Peter Pan".
Note:When specifying multiple sort columns, the result set is first sorted by the first column, and then sorted by the second column of the sorted list, and so on.