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
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?
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.
ORDER BYto sort rows.
Define the sort order using
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.”
You are not signed in
Sign in to access coding exercises and test your new skills.