PostgreSQL ORDER BY Clause


PostgreSQL ORDER BY Clause

The PostgreSQL ORDER BY clause is used to sort the result set of a query by one or more columns. This clause is essential for organizing query results in a specific order, such as ascending or descending.


Syntax

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;

The ORDER BY clause has the following components:

  • column1, column2, ...: The columns to sort the result set by.
  • ASC: Optional. Sorts the result set in ascending order. This is the default order.
  • DESC: Optional. Sorts the result set in descending order.

Example PostgreSQL ORDER BY Clause Queries

Let's look at some examples of PostgreSQL ORDER BY clause queries:

1. Basic ORDER BY Example

SELECT first_name, last_name
FROM employees
ORDER BY last_name ASC;

This query retrieves the first_name and last_name columns from the employees table and sorts the results by last_name in ascending order.

2. ORDER BY Multiple Columns

SELECT first_name, last_name, salary
FROM employees
ORDER BY department_id ASC, salary DESC;

This query retrieves the first_name, last_name, and salary columns from the employees table and sorts the results first by department_id in ascending order and then by salary in descending order within each department.

3. ORDER BY with Aliases

SELECT first_name, last_name, salary * 1.1 AS new_salary
FROM employees
ORDER BY new_salary DESC;

This query retrieves the first_name, last_name, and a calculated column new_salary from the employees table and sorts the results by new_salary in descending order.

4. ORDER BY with NULLS FIRST/LAST

SELECT first_name, last_name, hire_date
FROM employees
ORDER BY hire_date ASC NULLS LAST;

This query retrieves the first_name, last_name, and hire_date columns from the employees table and sorts the results by hire_date in ascending order, placing any NULL values at the end.


Full Example

Let's go through a complete example that includes creating a table, inserting data, and using the ORDER BY clause to sort data.

Step 1: Creating a Table

This step involves creating a new table named employees to store employee data.

CREATE TABLE employees (
    id SERIAL PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    email VARCHAR(100),
    salary NUMERIC(10, 2),
    department_id INT,
    hire_date DATE
);

In this example, we create a table named employees with columns for id, first_name, last_name, email, salary, department_id, and hire_date.

PostgreSQL WHERE Clause

Step 2: Inserting Data into the Table

This step involves inserting some sample data into the employees table.

INSERT INTO employees (first_name, last_name, email, salary, department_id, hire_date)
VALUES ('John', 'Doe', 'john.doe@example.com', 50000, 1, '2020-01-15');

INSERT INTO employees (first_name, last_name, email, salary, department_id, hire_date)
VALUES ('Jane', 'Smith', 'jane.smith@example.com', 60000, 2, '2019-11-30');

INSERT INTO employees (first_name, last_name, email, salary, department_id, hire_date)
VALUES ('Jim', 'Brown', 'jim.brown@example.com', 55000, 1, NULL);

Here, we insert data into the employees table.

PostgreSQL WHERE Clause

Step 3: Sorting Data with ORDER BY

This step involves using the ORDER BY clause to sort data in the employees table.

Basic ORDER BY

SELECT first_name, last_name
FROM employees
ORDER BY last_name ASC;
PostgreSQL WHERE Clause

ORDER BY Multiple Columns

SELECT first_name, last_name, salary
FROM employees
ORDER BY department_id ASC, salary DESC;
PostgreSQL WHERE Clause

ORDER BY with Aliases

SELECT first_name, last_name, salary * 1.1 AS new_salary
FROM employees
ORDER BY new_salary DESC;
PostgreSQL WHERE Clause

ORDER BY with NULLS FIRST/LAST

SELECT first_name, last_name, hire_date
FROM employees
ORDER BY hire_date ASC NULLS LAST;
PostgreSQL WHERE Clause

These queries demonstrate how to use the ORDER BY clause to sort data in the employees table, including sorting by a single column, multiple columns, aliases, and handling NULL values.

Conclusion

The PostgreSQL ORDER BY clause is a fundamental tool for organizing query results in a specific order. Understanding how to use the ORDER BY clause and its syntax is essential for effective data presentation and manipulation in PostgreSQL databases.