The ORDER BY clause in SQL Server

The ORDER BY clause is used to filter records in SQL Server's returned result set. This clause can only be used in the SELECT statement.

ORDER BY clause syntax

 SELE CT 'biểu thức' 
FROM 'bảng'
[WHERE 'điều kiện']
ORDER BY 'bi knowledgeable' [ASC | DESC];

Variable names and variable values

Expression - column or calculation value you want to retrieve.

Table - the table you want to use to retrieve the record. Must have at least 1 table in the FROM clause.

WHERE 'condition' - optional. Conditions must be met, the new record is selected.

ASC - optional. Filter results in ascending order of expressions (default if not specified).

DESC - optional. Filter the results in descending order of expressions.

Note:

If ASC or DESC is not selected in the ORDER BY clause, the result will be sorted by ascending order by default, equivalent to ORDER BY 'ASC expression'.

For example - filtering without using the ASC / DESC attribute

 SELECT cough 
FROM nhanvien
WHERE nhanvien_id > 1000
ORDER BY ho ;

The returned result will be the records filtered by the employee's surname field, in ascending order, equivalent to the following clause.

 SELECT cough 
FROM nhanvien
WHERE nhanvien_id > 1000
ORDER BY ho ASC;

Most developers remove the ASC attribute if they want to sort in ascending order.

For example - sort in descending order

 SELECT cough 
FROM nhanvien
WHERE ten = 'Sarah'
ORDER BY ho DES C;

As a result, records filter by employee's surname in descending order.

For example - filtering by relative position

You can use the ORDER BY clause in SQL Server to filter by relative position in the result set, where the first field is set to 1, followed by 2 and so on .

 SELECT ho 
FROM nhanvien
WHERE ho = 'Anderson'
ORDER BY 1 DESC;

In this example, the returned result is the record of the employee's last name field in descending order. Since the employee surname is at the 1st position in the result set, the above result is the same as in the ORDER BY clause below.

 SELECT cough 
FROM nhanvien
WHERE ho = 'Anderson'
ORDER BY ho DESC ;

For example - use both ASC and DESC attributes

 SELECT h o, ten 
FROM nhanvien
WHERE ho = 'Johnson'
ORDER BY ho D ESC, ten ASC;

In the above example, the return record will be the employee surname arranged in descending order and the employee name in ascending order.

Previous article: WHERE clause in SQL Server

Next lesson: AND condition in SQL Server

3.5 ★ | 2 Vote

May be interested

  • The clause to combine JOIN data in SQLThe clause to combine JOIN data in SQL
    in sql, the join clause is used to combine records from two or more tables in a database using common values ​​from each table.
  • Attach database in Microsoft SQL Server 2008Attach database in Microsoft SQL Server 2008
    this article will illustrate the different usage methods of the 'for attach' clause to overcome the limitations encountered when using sp_attach_db and sp_attach_single_file_db.
  • HAVING clause in SQLHAVING clause in SQL
    the having clause in sql is used to filter records and retrieve only those records that match the requirements or are actually needed.
  • Interesting Facts About The Santa ClauseInteresting Facts About The Santa Clause
    the santa clause is one of the best christmas movies of all time. however, behind this christmas movie are surprising, little-known facts about the making process.
  • GROUP BY command in SQLGROUP BY command in SQL
    the group by clause in sql is used in conjunction with the select statement to sort data uniformly into groups.
  • How to Set up an FTP Server in Ubuntu LinuxHow to Set up an FTP Server in Ubuntu Linux
    this wikihow article will show you how to set up and connect to an ftp server from your ubuntu linux computer. ftp servers are useful for storing files from your computer and allowing others to browse them. in order to set up an ftp server...
  • The difference between web server and app serverThe difference between web server and app server
    you have probably seen that the terms web server and app server are often used interchangeably as if they are related to the same thing and also facilitate the website to function properly. but in reality, they are not the same.
  • How to stack cable bundles in the server roomHow to stack cable bundles in the server room
    the sloppy technicians will make the room the server system into a dangerous barbed wire, but the simple tip below will restore order for this important data container.
  • Network basics: Part 3 - DNS ServerNetwork basics: Part 3 - DNS Server
    a dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
  • Capture execution diagrams with SQL Server 2005 ProfilerCapture execution diagrams with SQL Server 2005 Profiler
    executable diagrams are one of the best tools for diagnosing query errors in order to adjust the execution of queries on sql server. in previous versions of sql server 2005, the only option for query performance was text diagrams or graph execution diagrams for manual queries. on sql server 2005, you can capture execution plans in two ways