That’s it with
WHERE and its operators. Moving on!
It is often useful to list the data in our result set in a particular order.
We can sort the results using
ORDER BY, either alphabetically or numerically. Sorting the results often makes the data more useful and easier to analyze.
For example, if we want to sort everything by the movie’s title from A through Z:
SELECT * FROM movies ORDER BY name;
ORDER BYis a clause that indicates you want to sort the result set by a particular column.
nameis the specified column.
Sometimes we want to sort things in a decreasing order. For example, if we want to select all of the well-received movies, sorted from highest to lowest by their year:
SELECT * FROM movies WHERE imdb_rating > 8 ORDER BY year DESC;
DESCis a keyword used in
ORDER BYto sort the results in descending order (high to low or Z-A).
ASCis a keyword used in
ORDER BYto sort the results in ascending order (low to high or A-Z).
The column that we
ORDER BY doesn’t even have to be one of the columns that we’re displaying.
ORDER BY always goes after
WHERE is present).
Suppose we want to retrieve the
year columns of all the movies, ordered by their name alphabetically.
Type the following code:
SELECT name, year FROM movies ORDER BY name;
Your turn! Remove the previous query.
Write a new query that retrieves the
imdb_rating columns of all the movies, ordered highest to lowest by their ratings.