Sort results in SQL

To sort data in SQL, we use the ORDER BY clause .

ORDER BY is used to sort data in ascending order or in descending order on one or more columns. Some databases sort the default query results in ascending order.

Syntax

The basic syntax of the ORDER BY clause is used to sort results in ascending or descending order as follows:

 SELECT danhsach_cot 
FROM ten_bang
[WHERE dieu_kien]
[ORDER BY cot1, cot2, . cotN] [ASC | DESC];

Note :

  1. 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] .
  2. You can use more than one column in this ORDER BY clause. Note that any column you are using to sort must be in the list_cach.

For example

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 | Haiduong | 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 | Haiduong | 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 | Haiduong | 10000.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | +----+----------+-----+-----------+----------+

To retrieve rows with their own priority order, the SELECT query will look like this:

 SQL> SELECT * FROM NHANVIEN 
ORDER BY (CASE DIACHI
WHEN 'Hanoi' THEN 1
WHEN 'Haiduong' THEN 2
WHEN 'HCM' THEN 3
ELSE 100 END) ASC, DIACHI DESC;

The result is:

 +----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 2 | Loan | 25 | Hanoi | 1500.00 | | 7 | Lam | 24 | Haiduong | 10000.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 1 | Thanh | 32 | Haiphong | 2000.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 4 | Manh | 25 | Hue | 6500.00 | +----+----------+-----+-----------+----------+

First, we can arrange employees according to DIACHI with their own order, then the remaining DIACHI will be arranged naturally in the reverse order of the alphabet.

In the next section, we will learn about SQL constraints, remember to follow them.

Previous article: DISTINCT keyword in SQL

Next article: Constraints in SQL

5 ★ | 1 Vote

May be interested

  • MS Excel 2007 - Lesson 8: Sort and FilterMS Excel 2007 - Lesson 8: Sort and Filter
    sort and filter are features that allow you to manipulate data in a spreadsheet based on standards. to sort data and filter data in excel 2007 you will use the sort & filter feature. below tipsmake.com will guide you how to filter data, arrange data with this feature in excel 2007.
  • Excel does not have Sort Oldest to Newest, what should I do?Excel does not have Sort Oldest to Newest, what should I do?
    you are sorting time data but excel does not have sort oldest to newest. fix the display error problem with tipsmake now.
  • How to sort dates in ascending and descending ways in ExcelHow to sort dates in ascending and descending ways in Excel
    how to sort dates in ascending and descending ways in excel. the following article helps you to sort dates in ascending or descending order in excel quickly and effectively. method 1: step 1: select the data column containing dates to sort - on the data tab
  • How to Sort Alphabetically in Microsoft WordHow to Sort Alphabetically in Microsoft Word
    alphabetizing is a skill worth learning in word, especially if you often work with tables of contents and lists. luckily, once you know it, the process is very simple. this guide will help you learn how to sort lists alphabetically on any version of word.
  • Selection sort algorithm (Selection Sort)Selection sort algorithm (Selection Sort)
    selection sort is a simple algorithm. this sorting algorithm is an algorithm based on in-place comparison, in which the list is divided into two parts, sorted (list) on the left and unsorted (unsorted list) in the right. initially, the sorted part is blank and the unordered part is the original list.
  • How to Sort Gmail by SenderHow to Sort Gmail by Sender
    this is an article that shows you how to sort emails by sender in gmail using inbox search methods. note that these are only alternatives; gmail doesn't allow you to sort your entire inbox by sender. however, you can find a way to see all emails by sender.
  • Bubble Sort (Bubble Sort)Bubble Sort (Bubble Sort)
    bubble arrangement is a simple sort algorithm. this sorting algorithm is carried out based on comparing pairs of adjacent and swap elements if they are not in order.
  • Insert algorithm (Insertion Sort)Insert algorithm (Insertion Sort)
    sort insertion is a sorting algorithm based on in-place comparison. here, a sub-list is always maintained in sorted form. inserting is inserting an element into the sorted list of children. the element is inserted in the appropriate position so that the list is still in order.
  • Sort and sort files in Lion FinderSort and sort files in Lion Finder
    some sorting options are not always available in all finder views. sometimes you can combine them; sometimes can't.
  • How to Sort Microsoft Excel Columns AlphabeticallyHow to Sort Microsoft Excel Columns Alphabetically
    microsoft excel is a great tool for organizing your information. here's a guide to a basic but extremely useful function, sorting your data alphabetically. format the header row. the header row is the top row of your spreadsheet, with the...