-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrder BY Operator
More file actions
41 lines (27 loc) · 1.01 KB
/
Copy pathOrder BY Operator
File metadata and controls
41 lines (27 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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.
Example:
If we want to sort everything by the movie’s title from A through Z:
SELECT *
FROM movies
ORDER BY name;
ORDER BY is a clause that indicates you want to sort the result set by a particular column.
name is the specified column.
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;
DESC is a keyword used in ORDER BY to sort the results in descending order (high to low or Z-A).
ASC is a keyword used in ORDER BY to sort the results in ascending order (low to high or A-Z).
Example:
Suppose we want to retrieve the name and year columns of all the movies, ordered by their name alphabetically.
Type the following code:
SELECT name, year
FROM movies
ORDER BY name;
Example:
SELECT name, year, imdb_rating
FROM movies
ORDER BY imdb_rating DESC;