ORDER BY command in SQL
In SQL, the ORDER BY clause is used to sort data in ascending order or in descending order on one or more columns. Some databases arrange query results in the default ascending order.
In particular, the ASC command is used to sort up and DESC is used to sort descending.
In this article, we'll show you in detail how to use SQL ORDER BY clauses with syntax and specific examples to make it easier to visualize and capture statements.
Syntax ORDER BY command in SQL
The basic syntax of the ORDER BY command with the SELECT statement will be as follows:
SELECT danhsach_cot
FROM ten_bang
[WHERE dieu_kien]
[ORDER BY cot1, cot2, . cotN] [ASC | DESC];
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 cot1, cot2, . cotN] [ASC].
Example of ORDER BY in SQL
Suppose the NHANVIEN table has the following records:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
The following example illustrates how to sort in ascending order for TEN and LUONG.
SQL> SELECT * FROM NHANVIEN
ORDER BY TEN, LUONG;
The above example returns the result:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 6 | Cao | 22 | HCM | 4500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 1 | Thanh | 32 | Haiphong | 2000.00 | +----+----------+-----+-----------+----------+
The following block will sort the results in descending order by TEN field.
SQL> SELECT * FROM NHANVIEN
ORDER BY TEN DESC;
The result is:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | +----+----------+-----+-----------+----------+
In the next section, we will learn about the GROUP BY statement, please remember to follow it.
Previous post: TOP command in SQL
Next lesson: GROUP BY statement in SQL
You should read it
- Sort records in MongoDB
- How to order food on the phone with Now.vn application
- How to change TubeMate download order
- Instructions for ordering food through Zalo
- The order in which objects appear in Powerpoint
- How to rearrange the order of the watch faces on Apple Watch
- Relax with 3 ways to print in this simple reverse order
- How to sort data in Excel Ascending and descending, according to ABC
May be interested
- Order bitsadmin getnotifycmdline, getnotifyflags, getnotifyinterfacetoday's article will introduce 3 commands: bitsadmin getnotifycmdline, bitsadmin getnotifyflags and bitsadmin getnotifyinterface in windows.
- Doskey command in Windowsthe doskey command calls doskey.exe (which reminds of previously entered command lines), modifies the command line and creates macros.
- 10 useful commands in Windows you should knowcommand prompt is used to execute batch files, perform tasks quickly, help you troubleshoot and solve some windows problems when the system crashes. however, not all commands in windows are useful and must be done regularly. with the 10 command lines in the following article it is very useful even if you are not an administrator.
- How to order food on the phone with Now.vn applicationwith now.vn you can order and choose a variety of food options with a wide variety of regional specialties.
- How to use the which command in Linuxthe which command in linux determines the executable binary, which will execute when you issue a command to the shell. if you have different versions of the same program on your computer, you can use which to find out which shell will use.
- The command reg add in Windowsthe reg add command adds new subkeys or entries to the registry.
- Exit command in Windowsthe exit command exits the cmd.exe program (command interpreter) or the current batch script. to learn how to use this command, please see the example below.
- Rem command in Windowsthe rem command helps write comments (comments) in a batch file or config.sys.
- The dd command in Linux, How to use the dd commanddd is a command line utility for unix-like and unix operating systems, with the main purpose of converting and copying files.
- How to use Xcopy command in Command Promptknowing how to use xcopy command in command prompt, you will easily use commands in cmd professionally. in particular, the xcopy command is extremely useful for those who copy and copy data on the computer.