In all the previous examples you retrieved data without specifying any specific sort order. To sort the fetched data in some desired order, you use the ORDER BY clause. By default this clause sorts data in ascending order. You can use the DESC option to have the output in descending order. If used, this clause must be placed last in the SELECT statement. The following query is sorted on the last name of employees. Another alternate is to specify the position of the columns you wish to sort data on. For instance, rather than entering the column name (LAST_NAME), you can use its position like this: ORDER BY 1. Moreover, you can add as many columns to the ORDER BY clause as there are number of columns in a table. To sort by multiple columns, simply specify the column names separated by commas (just as you do when you are selecting multiple columns). For example, if you are displaying an employee list, you might want to display it sorted by last name and first name (first by last name, and then within each last name sort by first name).
NOTE
It is
legal to sort the output by a column that is not retrieved in your query. In
the case of multiple columns, the output is displayed exactly in the order of
the sort sequence specified. For example, if you sort the query by last name
and department id, the result will be sorted first by the last name and then by
the department id.
SQL Statement :
SELECT last_name, department_id, hire_date
FROM employees
ORDER BY
last_name;
Output :
No comments:
Post a Comment