4) Using SQL ORDER BY to sort by dates exampleīesides the character and numeric data, you can use the ORDER BY clause to sort rows by date. Salary DESC Code language: SQL (Structured Query Language) ( sql ) The following example uses the ORDER BY clause to sort employees by salary from high to low: SELECT 3) Using SQL ORDER BY clause to sort values in a numeric column example Notice the change in position of two employees: Alexander Khoo and Alexander Hunold in the result set. In this example, the ORDER BY clause sorts the result set by the first name in ascending order, then it sorts the sorted result set by the last name in descending order. Last_name DESC Code language: SQL (Structured Query Language) ( sql ) The following example uses the ORDER BY clause to sort the employees by the first name in ascending order and the last name in descending order: SELECT 2) Using SQL ORDER BY clause to sort values in multiple columns example The ORDER BY sorts the rows by the values in the first_name column. The following example uses the ORDER BY clause to sort employees by first names in alphabetical order: SELECTįirst_name Code language: SQL (Structured Query Language) ( sql ) | 108 | Nancy | Greenberg | | 12000.00 |Īs you can see clearly from the output, the rows do not have any order. | employee_id | first_name | last_name | hire_date | salary | The following SELECT statement returns the data from the employee id, first name, last name, hire date, and salary column of the employees table: SELECTĮmployees Code language: SQL (Structured Query Language) ( sql ) ![]() 1) Using SQL ORDER BY clause to sort values in one column example We’ll use the employees table in the sample database for the demonstration. It means that the rows in the result set don’t have a specific order. Note that if you don’t specify the ORDER BY clause, the SELECT statement will not sort the result set. In this syntax, the ORDER BY clause sorts the result set by the sort_expression_1 first, and then sorts the sorted result set by the sort_expression_2. Sort_expression_2 Code language: CSS ( css ) In this case, you need to use a comma to separate two sort expressions: SELECT select_list FROM table_name ORDER BY sort_expression_1 , The ORDER BY clause also allows you to sort the result set by multiple expressions. Note that the ORDER BY clause uses the ASC option by default if you don’t either ASC or DESC. Third, use ASC option to sort the result set by the sort expression in ascending order and DESC to sort the result set by the sort expression in the descending order.The sort expression specifies the sort criteria. Second, specify a sort expression after the ORDER BY clause. ![]() ![]() The database will evaluate the SELECT statement with the ORDER BY * clause in the following order: FROM > SELECT > ORDER BY. First, place the ORDER BY clause after the FROM clause. ![]() Sort_expression Code language: SQL (Structured Query Language) ( sql ) The following shows the syntax of the ORDER BY clause: SELECT The ORDER BY clause allows you to sort the rows returned by the SELECT clause by one or more sort expressions in ascending or descending order. The ORDER BY is an optional clause of the SELECT statement. Summary: This tutorial shows you how to use the SQL ORDER BY clause to sort the result set based on specified criteria in ascending or descending orders.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |