SQL
MySQL Functions: String, Numeric, User-Defined, Stored
What are functions? MySQL can do much more than just store and retrieve data . We can also perform...
Using the SELECT command, results were returned in the same order the records were added into the database. This is the default sort order. In this section, we will be looking at how we can sort our query results. Sorting is simply re-arranging our query results in a specified way. Sorting can be performed on a single column or on more than one column. It can be done on number, strings as well as date data types.
MySQL ORDER BY is used in conjunction with the SELECT query to sort data in an orderly manner. The MySQL ORDER BY clause is used to sort the query result sets in either ascending or descending order.
SELECT statement... [WHERE condition | GROUP BY `field_name(s)` HAVING condition] ORDER BY `field_name(s)` [ASC | DESC];
HERE
It is used to sort the query results in a top to bottom style. | It is used to sort the query results in a bottom to top style |
When working on date data types, the earliest date is shown on top of the list. | . When working on date types, the latest date is shown on top of the list. |
When working with numeric data types, the lowest values are shown on top of the list. | When working with numeric data types, the highest values are shown at top of the query result set. |
When working with string data types, the query result set is sorted from those starting with the letter A going up to the letter Z. | When working with string data types, the query result set is sorted from those starting with the letter Z going down to the letter A. |
Both the SQL DESC and ASC keywords are used together in conjunction with the SELECT statement and MySQL ORDER BY clause.
DESC and ASC syntax
The SQL DESC sort keyword has the following basic syntax.
SELECT {fieldName(s) | *} FROM tableName(s) [WHERE condition] ORDER BY fieldname(s) ASC /DESC [LIMIT N] HERE
Examples:
Let's now look at a practical example -
SELECT * FROM members;
Executing the above script in MySQL workbench against the myflixdb gives us the following results shown below.
membership_number full_names gender date_of_birth physical_address postal_address contct_number 1 Janet Jones Female 21-07-1980 First Street Plot No 4 Private Bag 0759 253 542 This email address is being protected from spambots. You need JavaScript enabled to view it. 2 Janet Smith Jones Female 23-06-1980 Melrose 123 NULL NULL This email address is being protected from spambots. You need JavaScript enabled to view it. 3 Robert Phil Male 12-07-1989 3rd Street 34 NULL 12345 This email address is being protected from spambots. You need JavaScript enabled to view it. 4 Gloria Williams Female 14-02-1984 2nd Street 23 NULL NULL NULL 5 Leonard Hofstadter Male NULL Woodcrest NULL 845738767 NULL 6 Sheldon Cooper Male NULL Woodcrest NULL 976736763 NULL 7 Rajesh Koothrappali Male NULL Woodcrest NULL 938867763 NULL 8 Leslie Winkle Male 14-02-1984 Woodcrest NULL 987636553 NULL 9 Howard Wolowitz Male 24-08-1981 SouthPark P.O. Box 4563 987786553
Let's suppose the marketing department wants the members details arranged in decreasing order of Date of Birth. This will help them send birthday greetings in a timely fashion. We can get the said list by executing a query like below -
SELECT * FROM members ORDER BY date_of_birth DESC;
Executing the above script in MySQL workbench against the myflixdb gives us the following results shown below.
The same query in ascending order
SELECT * FROM members ORDER BY date_of_birth ASC
Note: NULL values means no values (not zero or empty string) . Observe the way they have been sorted.
Let's consider the following script that lists all the member records.
SELECT * FROM `members`;
Executing the above script gives the following results shown below.
membership_number full_names gender date_of_birth physical_address postal_address contct_number 1 Janet Jones Female 21-07-1980 First Street Plot No 4 Private Bag 0759 253 542 This email address is being protected from spambots. You need JavaScript enabled to view it. 2 Janet Smith Jones Female 23-06-1980 Melrose 123 NULL NULL This email address is being protected from spambots. You need JavaScript enabled to view it. 3 Robert Phil Male 12-07-1989 3rd Street 34 NULL 12345 This email address is being protected from spambots. You need JavaScript enabled to view it. 4 Gloria Williams Female 14-02-1984 2nd Street 23 NULL NULL NULL 5 Leonard Hofstadter Male NULL Woodcrest NULL 845738767 NULL 6 Sheldon Cooper Male NULL Woodcrest NULL 976736763 NULL 7 Rajesh Koothrappali Male NULL Woodcrest NULL 938867763 NULL 8 Leslie Winkle Male 14-02-1984 Woodcrest NULL 987636553 NULL 9 Howard Wolowitz Male 24-08-1981 SouthPark P.O. Box 4563 987786553 NULL
Suppose we want to get a list that sorts the query result set using the gender field, we would use the script shown below.
SELECT * FROM `members` ORDER BY `gender`;
membership_number full_names gender date_of_birth physical_address postal_address contct_number 1 Janet Jones Female 21-07-1980 First Street Plot No 4 Private Bag 0759 253 542 This email address is being protected from spambots. You need JavaScript enabled to view it. 2 Janet Smith Jones Female 23-06-1980 Melrose 123 NULL NULL This email address is being protected from spambots. You need JavaScript enabled to view it. 4 Gloria Williams Female 14-02-1984 2nd Street 23 NULL NULL NULL 3 Robert Phil Male 12-07-1989 3rd Street 34 NULL 12345 This email address is being protected from spambots. You need JavaScript enabled to view it. 5 Leonard Hofstadter Male NULL Woodcrest NULL 845738767 NULL 6 Sheldon Cooper Male NULL Woodcrest NULL 976736763 NULL 7 Rajesh Koothrappali Male NULL Woodcrest NULL 938867763 NULL 8 Leslie Winkle Male 14-02-1984 Woodcrest NULL 987636553 NULL 9 Howard Wolowitz Male 24-08-1981 SouthPark P.O. Box 4563 987786553 NULL
"Female" members have been displayed first followed by "Male" members, this is because when ORDER BY DESC clause is used without specifying the ASC or MySQL DESC keyword, by default, MySQL has sorted the query result set in an ascending order.
Let's now look at an example that does the sorting using two columns; the first one is sorted in ascending order by default while the second column is sorted in descending order.
SELECT * FROM `members` ORDER BY `gender`,`date_of_birth` DESC;
Executing the above script in MySQL workbench against the myflixdb gives the following results.
The gender column was sorted in ascending order by default while the date of birth column was sorted in descending order explicitly
Suppose we want to print a payments history for a video library member to help answer queries from the front desk, wouldn't it be more logical to have the payments printed in a descending chronological order starting with the recent payment to the earlier payment?
DESC in SQL is a keyword which becomes handy in such situations. We can write a query that sorts the list in descending order using the payment date.
Suppose the marketing department wants to get a list of movies by category that members can use to decide which movies are available in the library when renting movies, wouldn't it be more logical to look sort the movie category names and title in ascending so that members can quickly lookup the information from the list?
The ASC keyword comes in handy in such situations; we can get the movies list sorted by category name and movie title in an ascending order.
What are functions? MySQL can do much more than just store and retrieve data . We can also perform...
What is Object Type in PL/SQL? Object-Oriented Programming is especially suited for building...
What are regular expressions? Regular Expressions help search data matching complex criteria. We...
What is Package in Oracle? PL/SQL package is a logical grouping of a related subprogram...
What is the UPDATE Query? UPDATE MySQL command is used to modify rows in a table. The update...
The data modification clauses in SQLite are INSERT, UPDATE, and DELETE statements. It is used for...