Available Tables

Next →

Sorting Data with ORDER BY

The ORDER BY clause allows you to sort query results by one or more columns. By default, it sorts in ascending order (ASC), but you can also sort in descending order using DESC.

Example: Sorting by One Column

To sort students by age from youngest to oldest:

SELECT * FROM students ORDER BY age;

To sort them from oldest to youngest:

SELECT * FROM students ORDER BY age DESC;

You can also sort by multiple columns. For example:

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

This sorts by grade (highest first), then by name alphabetically within each grade.