fg

fg

Posts

ORDER BY

Description:

-The order of rows that are returned in a query result is undefined. The ORDER BY clause can be used to sort the rows. However, if you use the ORDER BY clause, it must be the last clause of the SQL statement.

-Use the keywords NULLS FIRST or NULLS LAST to specify whether returned rows containing null values should appear first or last in the ordering sequence.

-You can also use a column alias in the ORDER BY clause

-You can also sort by a column that is not in the SELECT list.

-You can sort query results by specifying the numeric position of the column in the SELECT clause.

 
-You can sort query results by more than one column. Specify the columns and separate the column names using commas.

Syntax: 
 SELECT      expr
   FROM       table
   [WHERE       condition(s)]
   [ORDER BY  {column, expr, numeric_position} [ASC|DESC]];
 In the syntax:

ORDER BY   specifies the order in which the retrieved rows are displayed.

ASC             orders the rows in ascending order (this is the default order).

DESC           orders the rows in descending order.

Examples: 
Sorting in ascending order:
SELECT   last_name, job_id,hire_date

FROM     employees

ORDER BY hire_date ;
Sorting in descending order:
 SELECT   last_name, job_id,hire_date
FROM     employees

ORDER BY hire_date DESC ;

Sorting by using the column’s numeric position:
 SELECT   last_name, job_id, hire_date

FROM     employees

ORDER BY 2;

Sorting by multiple columns:
SELECT last_name, department_id, salary
FROM   employees
ORDER BY department_id, salary DESC;





No comments :

Post a Comment