When we simply query from a table, the result set is sorted by default based on the order of the inserted data. In most relational databases, a unique identifier is automatically generated through an auto-incremented value. In Postgres, this is called a serial. Since this value is automatically generated when data is inserted into a table, you will generally see the data sorted in this way. For example, if we query the invoice table, we will see it ordered automatically by the invoice_id.
SELECT *
FROM invoice;
The ORDER BY clause is useful for when we want to list records in a specific order. For example, If we wanted to sort the result based on the billing_country, we can add an ORDER BY clause to the SELECT statement after the FROM clause:
SELECT *
FROM invoice
ORDER BY billing_country;
Notice that the data is now sorted based on the billing_country. It starts with Argentina for the first row, continues with all of the other rows that have the value of Argentina, followed by Australia, and so forth. Also notice that the invoice_id is no longer in order, as the results have been reordered based on the billing_country.
Let's look at the customer table and see what the results would look like without ordering the data:
SELECT customer_id, first_name, last_name
FROM customer;
If we wanted to order it by the first_name, we can simply add the ORDER BY clause:
SELECT customer_id, first_name, last_name
FROM customer
ORDER BY first_name;
The result set in the customer table would now be sorted by the first_name column:
By default, when we use the ORDER BY clause in a SELECT statement, it sorts the data in ascending order. However, there are instances where we may want to sort the data in descending order. For example, if we wanted to query the invoice table and display the result set based on the total, we could sort it like this:
SELECT invoice_id, customer_id, total
FROM invoice
ORDER BY total;
Notice that this command returns 412 rows, with a lot of the initial rows starting with 1. What if we wanted to find the highest total first? We can add the keyword DESC to the ORDER BY clause after the column to sort it accordingly.
SELECT invoice_id, customer_id, total
FROM invoice
ORDER BY total DESC;
This would result in the following:
You can also use ASC in place of DESC to display the result in ascending order, although this won’t be any different than if you just listed the column without ASC added.
These two commands would result in the same result set, in the same order:
SELECT *
FROM invoice
ORDER BY total ASC;
And
SELECT *
FROM invoice
ORDER BY total;
We may want to sort the data by multiple parts in the same query. For example, think about listing customers in a phone directory. In that case, we would want to sort based on the last name, first name, and the company. We would do this in three stages. First, ORDER BY last_name. Then, within the matching last_name values, we would ORDER BY first_name. Next, within the matching last_name and first_name values, we would ORDER BY the company. This type of multilevel ordered sequence is called the cascading order sequence. It can easily be created by listing the columns, separated by commas, after the ORDER BY clause.
SELECT customer_id, last_name, first_name, company
FROM customer
ORDER BY last_name, first_name, company;
This cascading order sequence may not appear particularly useful for our data set, since we only have 59 customers in the table. However, with more customers, you will have many more identical first and last names, and multilevel sorting becomes more important.
Source: Vincent Tran
[MUSIC PLAYING] Another clause can be quite useful when it comes to SELECT statements is the ORDER BY by clause. It allows us to be able to sort database on the column sets that are being returned. For example, if we want to take the customer data here and then sort it base on the first name, we can enter in ORDER BY and then include in first_name. Go ahead and can Run. This will now return the result set entirely ordered based on the first name.
Now, we can also take the data and then order it based on multiple different criteria. So for example, if you had two errands and then we wanted to ensure that we also sort it based on last name, we can add it utilizing a comma, and then including in the next column associated with it. And utilizing that, it was sorted based on the first name first and then the last name if the first name was matching up inside.
The other option too is that you can also sort the data in descending order. By default, it is sorting it based on ascending order. So if we change this and turn in DESC, which stands for a descending, it'll now display it in descending order based on that first name. So it starts at the Wyatt, and then Victor, then Tim, and so forth.
[MUSIC PLAYING]