You are reading the article Order By Clause In Sql updated in September 2023 on the website Dacquyenphaidep.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Order By Clause In SqlIntroduction to ORDER BY Clause in SQL
Hadoop, Data Science, Statistics & othersParameters of ORDER BY Clause in SQL
column_name: This indicates the column name that we wish to retrieve based on which the data is to be arranged.
table_name: This indicates the name of the table from which the records are to be obtained. We need to have at least one table name in this parameter.
WHERE condition: This is an optional parameter. WHERE clause contains the conditions that we must meet for the data to be chosen.
ASC: This is the keyword used to sort the column data in ascending order. If no keyword is mentioned, data is sorted in ascending order by default.
DESC: This is the keyword used to sort the column data in descending order.ORDER BY Clause
Syntax of ORDER BY is:SELECT column_name(s) FROM table_name(s) [WHERE condition] The syntax for sorting data according to a single column
To sort data of the table based on a single column in either ascending or descending order, we can either utilize the ASC or DESC keywords. In our example, we will be sorting data in ascending order, thereby using the keyword ASC.
SyntaxSELECT * FROM table_name ORDER BY column_name ASC The syntax for sorting data according to several columns
To sort data of the table based on several columns in either ascending or descending order, we can either utilize the ASC or DESC keywords. To mention the several columns according to which we will be sorting data, we need to mention the names of the columns separated by the comma (,) operator. In our example, we will be sorting data in ascending order, thereby using the keyword ASC.
SyntaxSELECT * FROM table_name ORDER BY column1 ASC, column2 ASC Examples of ORDER BY Clause
Let us look at various examples to understand the ORDER BY Clause better.1. Example to sort the results in an ascending Order
To categorize the results in ascending order, we can use the ASC keyword. If no keyword, either ASC or DESC, has been provided, then the default sorting order is ascending order. Let us understand this with the help of an example. We have an Employee table.SELECT * FROM Employee ORDER BY EmployeeLastName;
This example gives back all the records from the Employee table sorted in ascending order according to the EmployeeLastName field. We can also use the keyword ASC as follows to get the same result.SELECT EmployeeID, EmployeeLastName FROM Employee ORDER BY 1 DESC; 2. Example to sort the results in a descending order
We use the keyword DESC when we want to sort our data in descending order, in our ORDER BY clause. Let us understand this with the help of an example. We have the same table of Employee containing the following data.
We need only Employees whose EmployeeID is greater than 2, and we need our data sorted in descending order. We use the following SQL statement for the same and get the result set table with only 4 records.SELECT * FROM Employee ORDER BY EmployeeID DESC; 3. Example to sort the results by their relative position
We can also arrange our data by the relative position of the columns, where 1 represents the first field, 2 represents the second field, 3 represents the third field and so on. Let us try to arrange data in our Employee table according to relative positions.
By using the SQL statement as follows, we can arrange the data in descending order of EmployeeID. We have also asserted that we need only two columns from the table, namely EmployeeID and EmployeeLastName, along with using the WHERE clause to mention that we do not want any row containing the EmployeeID 003.SELECT EmployeeID, EmployeeLastName FROM Employee ORDER BY 1 DESC;
Since the column at position 1 is EmployeeID, the result set gets sorted according to EmployeeID.Conclusion
In SQL, the SELECT statement does not return data in any specific order. To guarantee a particular order, we make use of the ORDER BY clause. ORDER BY sorts on the basis of one or more columns. Records are returned in either ascending or descending order. If ASC or DESC keyword hasn’t been provided, then the results will be categorized in ascending order.Recommended Article
We hope that this EDUCBA information on “ORDER BY Clause in SQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Order By Clause In Sql
Update the detailed information about Order By Clause In Sql on the Dacquyenphaidep.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!