Menu
Coddy logo textTech

Sort Results Part 1

Part of the Fundamentals section of Coddy's SQL journey — lesson 13 of 72.

When querying a database, organizing your results in a meaningful order can make data analysis much more efficient. To sort the result we use the ORDER BY keyword and after that, we should specify by which field we are ordering by. By default, it sorts by ascending order.

For example consider the following <strong>competition</strong> table:

runner_idageavg_speed
1473.65
2623.07
3576.82
4564.34
5254.93
6403.94
7236.58
8403.43
SELECT *
FROM competition
WHERE age > 50
ORDER BY avg_speed

This is the result

runner_idageavg_speed
2623.07
4564.34
3576.82

To specify how to sort that data we can add DESC or ASC keywords after the name of the column.

ORDER BY avg_speed ASC
challenge icon

Challenge

Easy

Available tables and columns:

  • <strong>feathers</strong>: <strong>id</strong>, <strong>weight</strong>

Return all of the ids after ordering them by the weight in descending order

Cheat sheet

Use ORDER BY to sort query results. By default, it sorts in ascending order:

SELECT *
FROM table_name
ORDER BY column_name

To specify sort direction, add ASC (ascending) or DESC (descending):

ORDER BY column_name DESC
ORDER BY column_name ASC

Try it yourself

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals