Back to AGGREGATIONS & GROUP BY, ORDER BY

ORDER BY

Understand ORDER BY clause for reordering result

2 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Ready to continue?

Mark this lesson as complete when you're ready to proceed.

Key moments

  1. Define ORDER BY — The SQL ORDER BY keyword is a powerful tool used to sort data in ascending or descending order.
  2. Basic Syntax Example — A simple query uses ORDER BY followed by the column name to sort the results.
  3. Default Sort Direction — By default, the ORDER BY clause sorts the data in ascending order (ASC).
  4. Using DESC Keyword — To sort data with the highest values first, you must add the DESC keyword.
  5. Sorting Text Values — ORDER BY can sort text values alphabetically, and DESC reverses this alphabetical order.
  6. Multi-Column Sort — You can sort by more than one column, where the second column acts as a tie-breaker for the first.
  7. Mixing ASC and DESC — It is possible to mix ascending and descending directives within a single multi-column sort.
  8. Final Reminder — Remember that you can sort by any column in the table and specify any order.
PDF notes

Frequently asked questions

Does ORDER BY affect the number of rows returned?

No, ORDER BY only changes the sequence in which the rows are presented, not the total count or content of the result set.

Can I sort by a column that I didn't select?

Yes, you can sort by any column in the table, even if it is not listed in the SELECT statement.

What happens to NULL values when sorting?

The placement of NULLs depends on the specific database system, but they are typically treated as either the lowest or highest possible values.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.