SQL ORDER BY Guide: Sort Data Easily
To rearrange the order of the list, you can use the ORDER BY clause to sort the result set. The specific steps are as follows:
- To sort in ascending order based on a certain column’s value, you can use the following statement:
SELECT * FROM table_name ORDER BY column_name ASC;
- To sort by a specific column in descending order, you can use the following statement:
SELECT * FROM table_name ORDER BY column_name DESC;
- If you want to sort by values of multiple columns, you can use multiple column names separated by commas.
SELECT * FROM table_name ORDER BY column_name1 ASC, column_name2 DESC;
- To use custom rules for sorting, you can utilize a CASE statement.
SELECT * FROM table_name
ORDER BY
CASE
WHEN column_name = 'value1' THEN 1
WHEN column_name = 'value2' THEN 2
ELSE 3
END;
With the above method, the list can be flexibly adjusted for sorting as needed.