• 20 hours
  • Medium

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 8/29/24

Sort your data using ORDER BY

In relational algebra, the order of the tuples in a table has no meaning or importance. This brings us to a difference between relational algebra and SQL:  SQL allows you to sort table rows.

The ORDER BY Clause

Here is what the ORDER BY clause looks like:

SELECT * FROM entity ORDER BY lifetime ;

This query returns all of the rows of the entity table, and ranks the companies according to their  lifetime  attribute.

By default, the results are sorted in ascending order, meaning from smallest to largest. The previous query is therefore equivalent to the following:

SELECT * FROM entity ORDER BY incorporation_date ASC ;

And to sort in descending order?

Simply replace  ASC  with  DESC :

SELECT * FROM entity ORDER BY incorporation_date DESC ;

Sort Multiple Columns

Sometimes we might want to specify multiple columns. This is possible, of course! You simply specify the names of the columns you would like to retrieve, separated by a comma.

Summary

  • Use  ORDER BY  to sort rows.

  • Define the sort order using  ASC  and  DESC  .

A Little Exercise

The database is online. Get some practice by rewriting the queries found in this chapter. Don’t hesitate to change them, play with them, and “see what happens.” ;)

Interactive Console

Example of certificate of achievement
Example of certificate of achievement