SQL 基础
SQL 简介
SQL 入门
SQL 语法
SQL 创建数据库
SQL 创建表
SQL Constraints
SQL Insert
SQL Select
SQL Where
SQL AND & OR
SQL IN & Between
SQL Order By
SQL Top/Limit
SQL Distinct
SQL Update
SQL Delete
SQL Truncate Table
SQL Drop
SQL 连接
SQL Joining Tables
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Cross Join
高级 SQL
SQL Union
SQL Like
SQL Alter Table
SQL Aliases
SQL Group By
SQL Having
SQL Create View
SQL Create Index
SQL Dates and Times
SQL Cloning Tables
SQL 临时表
SQL 子查询
SQL 注入
SQL 参考
SQL 数据类型
MySQL 数据类型
SQL Server 数据类型
SQL 方法
SQL Order By - SQL基础教程 - 笔下光年
网站首页
SQL Order By
In this tutorial you will learn how to sort data returned by a SELECT query in SQL. ## Ordering the Result Set Generally when you use the SELECT statement to fetch data from a table, the rows in result set are not in any particular order. If you want your result set in a particular order, you can specify the ORDER BY clause at the end of the statement which tells the server how to sort the data returned by the query. The default sorting order is ascending. ## Syntax The ORDER BY clause is used to sort the data returned by a query in ascending or descending order. The basic syntax of this clause can be given with: ```sql SELECT column_list FROM table_name ORDER BY column_name ASC|DESC; ``` Here, column_list are the names of columns/fields like name, age, country etc. of a database table whose values you want to fetch, whereas the column_name is name of the column you want to sort. Let's check out some examples that demonstrate how it actually works. Consider we've an employees table in our database that has 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 | +--------+--------------+------------+--------+---------+ ``` ### Sorting Single Column The following SQL statement will return all the employees from the employees table and orders the result set by the emp_name column in ascending order. ```sql SELECT * FROM employees ORDER BY emp_name ASC; ``` You can skip the ASC option and simply use the following syntax. It returns the same result set as previous statement, because the default sorting order is ascending: ```sql SELECT * FROM employees ORDER BY emp_name; ``` After executing the above command, you'll get the output something like this: ``` +--------+--------------+------------+--------+---------+ | 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 perform a sorting in descending order. The following statement will orders the result set by the numeric salary column in descending order. ```sql SELECT * FROM employees ORDER BY salary DESC; ``` This time, you'll get the result set something like this: ``` +--------+--------------+------------+--------+---------+ | 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 | +--------+--------------+------------+--------+---------+ ``` ## Sorting Multiple Columns You can also specify multiple columns while sorting. However, the change in result set will not visible until you've some duplicate values in your table. Well, let's find out: To understand the multi-column sorting in a better way, let's assume that we've a table named trainees in our database with 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 see the table carefully, you'll find that we've some duplicate values. However, the full name of the trainee "Peter Parker" and "Peter Pan" are different but their first names are same. Now execute the following command which orders the result set by the first_name column. ```sql SELECT * FROM trainees ORDER BY first_name; ``` After execution, you'll get the output something like this: ``` +----+------------+------------+-------------+--------+ | 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 execute this statement which orders the result set by first_name and last_name columns. ```sql 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 and the current result set — this time the record of the trainee "Peter Parker" comes after the "Peter Pan". Since the first name of both the trainees are same which is "Peter", so the second level ordering is performed at the last_name column for these two trainees that's why the record of the trainee "Peter Parker" comes after the "Peter Pan". <div class="callout callout-info mb-3">Note: When multiple sort columns are specified, the result set is initially sorted by the first column and then that ordered list is sorted by the second column, and so on.</div>
上一篇:
SQL IN & Between
下一篇:
SQL Top/Limit