The ORDER BY clause in SQL


To learn more about the ORDER BY clause in SQL, read along with the article below of Taimienphi.vn. In addition, readers can refer to some previous articles of Taimienphi.vn to learn more information about the WHERE, GROUP BY, … clauses in SQL.


The ORDER BY clause in SQL Used in the SELECT statement to sort results in ascending or descending order. Oracle sorts the query results in ascending order by default.

Syntax of ORDER BY clause in SQL to sort data

SELECT column-list

FROM table_name [WHERE condition]

[ORDER BY column1 [, column2, .. columnN] [DESC]];

Example ORDER BY clause in SQL

Here is the employee data sheet:

order by in sql 2

If you want to sort the employee list by salary, the ORDER BY command in SQL is written as follows:

SELECT name, salary FROM employee ORDER BY salary;

The output should look like the following:

order by in sql 3

The first query sorts the results by name and displays them.

Alternatively you can use multiple columns in the ORDER BY clause in SQL.

If you want to sort the employee table by name and salary, the ORDER BY query in SQL is:

SELECT name, salary FROM employee ORDER BY name, salary;

The output should look like the following:

order by in sql 4

Notice the ORDER BY clause in SQL

The columns specified in the ORDER BY clause are among the columns selected in the SELECT column list.

You can specify the position of a column in the SELECT list to represent the columns in the ORDER BY clause, instead of having to write the column name.

The above queries can also be written in the form:

SELECT name, salary FROM employee ORDER BY 1, 2;

By default, the ORDER BY clause in SQL will sort data in ascending order. If you want to sort in descending order, you must explicitly specify as in the query below:

SELECT name, salary

FROM employee

ORDER BY name, salary DESC;

The ORDER BY query in SQL above only sorts the salary column in descending order and name column in ascending order.

If you want to sort employees by name and salary in descending order, the ORDER BY query in SQL is written as follows:

SELECT name, salary

FROM employee

ORDER BY name DESC, salary DESC

Use the expression in the ORDER BY clause in SQL

The expressions in the ORDER BY clause in SQL are used in the SELECT statement.

For example: If you want to display all employees’ names, the current salary and only increase 20% of the salary for those employees with salary above 30000 in descending order, the SELECT command will be written as below:

SELECT name, salary, salary * 1.2 AS new_salary

FROM employee

WHERE salary * 1.2> 30000

ORDER BY new_salary DESC;

The output of the above query is as follows:

order by in sql 5

Above Taimienphi.vn just introduced you to the ORDER BY clause in SQL. In the next article, Taimienphi.vn will introduce you more about VIEW, GRANT, … in SQL., And especially CREATE statement in SQLPlease follow along.

https://thuthuat.taimienphi.vn/order-by-trong-sql-33280n.aspx
In addition, you can learn more articles related to SQL, SQL Server such as how to create, test and replaceChange the Identity column on Microsoft SQL Server to gain more knowledge about the language as well as this data management environment. Good luck !

.

Add a Comment

Your email address will not be published. Required fields are marked *