PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Tutorial / PostgreSQL ORDER BY

PostgreSQL ORDER BY

 Summary: in this tutorial, you will learn how to sort the result set returned from the SELECTstatement by using the PostgreSQL ORDER BY clause.

Introduction to PostgreSQL ORDER BY clause

When you query data from a table, PostgreSQL returns the rows in the order that they were inserted into the table. In order to sort the result set, you use the ORDER BY clause in the SELECT statement.

The ORDER BY clause allows you to sort the rows returned from the SELECT statement in ascending or descending order based on criteria specified by different criteria.

The following illustrates the syntax of the ORDER BY clause:

1
2
3
4
5
6
7
8
SELECT
column_1,
column_2
FROM
tbl_name
ORDER BY
column_1 ASC,
column_2 DESC;

Let’s examine the syntax of the ORDER BY clause in more detail:

  • Specify the column that you want to sort in the ORDER BY clause. If you sort the result set by multiple columns, use a comma to separate between two columns.
  • Use ASC to sort the result set in ascending order and DESCto sort the result set in descending order. If you leave it blank, the ORDER BYclause will use ASCby default.

Let’s take some examples of using the PostgreSQL ORDER BY clause.

PostgreSQL ORDER BY examples

The following query sorts customers by the first name in ascending order:

1
2
3
4
5
6
7
SELECT
  first_name,
  last_name
FROM
   customer
ORDER BY
   first_name ASC;

PostgreSQL ORDER BY sort customers by first name

Because ASCis used by default, you can omit it in the statement.

If you want to sort the customers by the last name in descending order, you can use the DESCkeyword as the following query:

1
2
3
4
5
6
7
SELECT
       first_name,
       last_name
FROM
       customer
ORDER BY
       last_name DESC;

PostgreSQL ORDER BY sort customers by last name

If you want to sort the customers by the first name in the ascending order first, and then sort the sorted result set by the last name in descending order to produce the final result set, you can use the following statement:

1
2
3
4
5
6
7
8
SELECT
first_name,
last_name
FROM
customer
ORDER BY
first_name ASC,
last_name DESC;

PostgreSQL ORDER BY sort customers by first name and last name

Notice that the standard ANS-SQL only allows you to sort rows based on the columns that appear in the SELECTclause. However, PostgreSQL allows you to sort result set  based on the columns that even does not appear in the selection list.

It is good practice to follow ANSI-SQL to make your code portable and adapt with the changes that potentially happen in the next release of PostgreSQL.

In this tutorial, we have shown you how to sort the result set in ascending and descending order based on one or more columns by using the PostgreSQL ORDER BY clause.

Related Tutorials

  • PostgreSQL LIMIT
Previous Tutorial: PostgreSQL SELECT DISTINCT
Next Tutorial: PostgreSQL WHERE

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2016 by PostgreSQL Tutorial Website. All Rights Reserved.