PostgreSQL Order By Clause

The ORDER BY clause specifies the sort order of query.
Each expression can be followed by an optional ASC or DESC keyword to set the sort direction to ascending or descending. ASC order is the default. Ascending order puts smaller values first, where “smaller” is defined in terms of the < operator. Similarly, descending order is determined with the > operator.
The NULLS FIRST and NULLS LAST options can be used to determine whether nulls appear before or after non-null values in the sort ordering. By default, null values sort as if larger than any non-null value; that is, NULLS FIRST is the default for DESC order, and NULLS LAST otherwise.

Syntax:

SELECT select_list FROM table_expression ORDER BY sort_expression1 [ASC | DESC] [NULLS { FIRST | LAST }] [, sort_expression2 [ASC | DESC] [NULLS { FIRST | LAST }] ...]

Examples:

Display the student table data order by student name.

select * from student order by student_name; student_name | student_id | class | dob --------------+------------+-------+-------------------- Ajay | 1004 | 4 | 21-AUG-12 00:00:00 Karunakar | 1001 | 1 | 12-DEC-15 00:00:00 Varshitha | 1002 | 2 | 12-DEC-14 00:00:00

Powered by k2schools