SELECT name, age FROM students
ORDER BY age;This sorts results by the age column in ascending order (default).
SELECT name, age FROM students
ORDER BY age DESC;Add DESC to sort in descending order. Use ASC explicitly to clarify ascending order if needed.
SELECT name, grade, age FROM students
ORDER BY grade DESC, age ASC;This sorts by grade (descending) and then by age (ascending) within each grade.
SELECT name, age, grade FROM students
ORDER BY 2 DESC;Here, 2 refers to the second column selected (age). This is less readable and not recommended.
SELECT name FROM users
ORDER BY LENGTH(name) ASC;This orders results based on the length of the name.
SELECT name, score * 1.1 AS adjusted_score FROM students
ORDER BY adjusted_score DESC;You can sort by an alias defined in the SELECT clause.
Ask the AI if you need help understanding or want to dive deeper in any topic