1. Sorting Data
If you do not sort, the data will generally appear in the order in which it appears in the underlying table, which may be the order in which the data was originally added to the table. However, if the data is subsequently updated or deleted, the order is affected by how the DBMS reuses the storage space.
Clauses: SQL statements consist of clauses, some are necessary, and others are optional. A clause is usually made up of keywords plus the data provided. For example, the FROM clause in the SELECT statement that we mentioned in our last note.
Ordering requires the ORDER BY clause
SELECT from ORDER by Prod_name;
Be sure to ensure that the ORDER BY clause is the last sentence
2. Sort by multiple columns
SELECTfrom ORDER by Prod_price,prod_name;
Sort by Prod_price first, and then sort by prod_name
3. Sort By Column position
SELECT from ORDER by 2, 3;
2 refers to the second sort in the selected column first, and then the 3rd sort
4. Specify the sort direction
The order by default is in ascending order, and if descending, you need to specify the DESC keyword
SELECT from ORDER by DESC;
If you intend to sort by more than one column
SELECT from ORDER by DESC, Prod_name;
DESC is applied directly to the column in front of it, that is, prod_price, not to the Prod_name column, and if it is also followed by prod_name descending, add desc after it
The opposite is ASC, which specifies ascending
Database SQL statements Learn notes (3)-Sort Retrieve data