SQL ORDER BY Tutorial: Sorting Data

To write a sorting SQL statement, you can use the “ORDER BY” clause. Following the clause, you specify the column name you want to sort by, which can be one or more. You can also specify the sorting order, either ascending (ASC, default sorting order) or descending (DESC).

For instance, if you want to arrange the “students” table in ascending order based on the “age” column, you can write the following SQL statement:

SELECT * FROM students
ORDER BY age ASC;

If you want to sort in descending order by the “age” column, you can write the following SQL statement:

SELECT * FROM students
ORDER BY age DESC;

If you want to sort by multiple columns, you can list the order of these columns in the “ORDER BY” clause. For example, if you want to first sort in ascending order by the “age” column, and then in descending order by the “name” column, you can write the following SQL statement:

SELECT * FROM students
ORDER BY age ASC, name DESC;

By using the “ORDER BY” clause, you can sort the query results based on specific criteria to better meet your needs.

bannerAds