There are several clauses available in sequel for sorting and filtering data in a table. One of the most useful of all the clauses is the ORDER BY Clause. With this clause, you can reorder the data in a table by one or more columns. For example, you have a database of college students where each of them has a date of birth, using the ORDER BY Clause, you can sort their ages from oldest to youngest or from the youngest to the oldest.
The ORDER BY Clause is an optional clause that can be added to a SELECT statement. The purpose or main purpose of the clause is to help sort data in either ascending or descending order. For example: You can set a list of students name in alphabetic order from A – Z or vice versa.
Using The ORDER BY Clause For A Single Column
To do this, you begin with a SELECT statement, then the list of the columns to be sorted with each one separated by a comma. Next is the FROM keyword followed by the name of the table to be sorted. Finally, we have the ORDER BY clause followed by the name of the column to be sorted and then after adding a comma, specify how you want the column to be arranged either ASC for ascending or DESC for descending. To understand better, take a look at the syntax written below:
SELECT column_1, column_2, column_3 FROM table_name ORDER BY column_name ASC:
To select all columns in a table, you can make use of the asteriks (*) symbol. For example:
SELECT * FROM table_name ORDER BY column_name DESC;
Using The ORDER BY Clause For Multiple Columns
You can also order at a time multiple columns using the same syntax but slightly different. To do this, you use the following syntax:
SELECT column_1, column_2, column_3 FROM table_name ORDER BY column1_name ASC, column2_name DESC;
When using this though, make sure to type each column after the ORDER BY clause and separate them using a comma while also specifying whether you want the columns to be in ascending or descending order.
By reading and studying this article, you are now able to demonstrate the purpose of the ORDER BY Clause used for sorting data and also explain the different forms in which the ORDER BY Clause can be used to sort data. I here by urge you to practice not once, not twice but multiple times to get better. Good Luck!
Introduction In the fast paced world of technology, learning a versatile and high-in-demand programming language…
Introduction User Authentication policy is a very crucial process for every application and organization. It…
Introduction In previous articles, we have learnt about Django, how it works and how we…
Introduction In this article, we shall learn how to build and implement a blog app.…
Introduction In this article, we shall use a database for the first time to build…
Introduction In this article, we will build a pages app that has a homepage and…