Page Content

Tutorials

What Is ORDER BY Clause in PostgreSQL With Example

ORDER BY Clause

The ORDER BY clause in PostgreSQL sorts query results in a given order. Without an ORDER BY clause, rows are delivered in an unspecified order based on physical disc positioning or join algorithms and should not be depended on for consistency.

One or more sorting expressions can be column names, output column aliases, or output column numbers. By default, sorting is ascending (ASC), but adding the proper keyword after the expression changes it to descending (DESC). NULL values are larger than non-null values and sort last in ASC and first in DESC by default. NULLS FIRST and LAST can be used to control their placement. Sorting is based on the first expression, then the second expression for rows with equal values, and so on.

Combining LIMIT and OFFSET clauses to get a certain fraction of a result set requires the ORDER BY clause, as ignoring it results in an unpredictable subset of rows. PostgreSQL’s query optimiser considers LIMIT, which may cause different query plans and incorrect results without ORDER BY. Indexes, especially B-tree indexes, can speed up ORDER BY operations by storing entries in a sorted order and delivering data immediately in sequence. EXPLAIN may analyse the execution plan and determine if an index is used for sorting.

ORDER BY also affects aggregate and window functions. An ORDER BY phrase in the aggregate call for array_agg or string_agg can define the input value order, which can alter the output. Window methods like ROW_NUMBER() and running totals require organise BY in the OVER clause to organise rows within a partition. The ORDER BY phrase must be used at the end of a combined query with UNION, INTERSECT, or EXCEPT to sort the final result set, not individual subqueries. View definitions can include ORDER BY, but if the succeeding query overrides it, it may introduce needless sort operations, affecting speed.

ORDER BY Clause in PostgreSQL
ORDER BY Clause in PostgreSQL

Syntax and Basic Usage

Sort by expression1 using [ASC | DESC] [NULLS {FIRST | LAST}]. Expression 2: [ASC | DESC] [NULLS {FIRST | LAST}].

Expression: Any legal SQL phrase can be used here, including column names, aliases, and numeric SELECT list column references. PostgreSQL ORDER BY opens SQL to arbitrary phrases, including fields not in the SELECT list.

ASC (Ascending): The PostgreSQL ORDER BY clause uses ascending order (ASC) to sort query results from smallest to largest. The default sorting order is ASC if neither is given following a sorting statement. Sorting in ascending order shows smaller numbers first, with “smaller” defined by the < operator.

DESC (Descending): DESC is used in PostgreSQL’s ORDER BY clause to sort a result set from largest to smallest. Unless otherwise noted, this is the opposite of ascending order (ASC). When using DESC, “larger” is usually defined as >.

NULLS FIRST | NULLS LAST: Controls where NULL values appear in sorted output. By default, NULL values are higher. NULLS FIRST for descending, NULLS LAST for ascending is default.

Many expressions in the ORDER BY list cause hierarchical sorting. Beginning with expression 1, rows are organised. When many rows have expression 1, they are ordered by expression 2, etc. ASC/DESC sort direction and NULLS placement are assessed separately for each expression.

For example:

  • SELECT state* FROM friend ORDER;
  • A friend’s age-based order;
  • Select number_of_owners, manufacture_year, and trunc(mileage/1000) as kilometres from car_portal_app.car. Sort by manufacturer year, owners, and km;

Interaction with Other Clauses and Query Processing

LOGICALLY, the ORDER BY clause is run after the SELECT list creates an intermediate virtual table, filters (WHERE), groups (GROUP BY/HAVING), and combines queries.

WHERE Clause: If a WHERE clause is included in the SQL query, ORDER BY follows it. Before ORDER BY sorts data, WHERE filters it.

GROUP BY Clause: PostgreSQL’s GROUP BY clause groups rows with identical values in one or more columns into a summary row. To summarise calculations for each group rather than the total dataset, aggregate methods like COUNT, SUM, AVG, MAX, and MIN require this functionality. The SELECT list can only include grouping expressions or aggregate functions when GROUP BY is used because other non-grouped columns will have numerous values in a single group, causing an error.

LIMIT and OFFSET Clauses: Limit and offset clauses require ORDER BY. Retrieving “the tenth through twentieth rows” will yield an unexpected subset of the query’s total results without it because the order is not guaranteed.

Set Operations (UNION, INTERSECT, EXCEPT): Set procedures (UNION, INTERSECT, EXCEPT) require the ORDER BY clause at the end of the combined query. It covers all combined query results. ORDER BY inside set operation subqueries is frequently unhelpful because the outermost ORDER BY decides the final order.

Window Functions: The ORDER BY clause is a key part of window definitions (OVER clause). This determines the window function’s row processing order within a “window partition”. This internal ORDER BY does not always decide the query’s output order; a top-level phrase is needed.

Aggregate Functions (Ordered-Set Aggregates): The word “aggregate functions” refers to ordered-set aggregates like percentile_cont or mode that require an ORDER BY clause in the aggregate function call (e.g., WITHIN PAGE). The aggregate logic depends on the order of data entry for computation. String_agg, array_agg, and xmlagg additionally include an ORDER BY clause in their inputs to ensure consistent output based on input order.

Code Example:

-- 1. Create table and insert data (only 1 employee)
DROP TABLE IF EXISTS employees;
CREATE TABLE employees (
    id SERIAL PRIMARY KEY,
    name TEXT,
    department TEXT,
    salary NUMERIC,
    hire_date DATE,
    skills TEXT[],
    manager_id INT
);
INSERT INTO employees (name, department, salary, hire_date, skills, manager_id) VALUES
('Alice', 'HR', 50000, '2020-01-10', ARRAY['communication','recruitment'], NULL);
SELECT id, name, department, salary
FROM employees
WHERE salary > 40000
ORDER BY salary DESC;

Output:

DROP TABLE
CREATE TABLE
INSERT 0 1
 id | name  | department | salary 
----+-------+------------+--------
  1 | Alice | HR         |  50000
(1 row)

Performance and Optimization

PostgreSQL query planner optimises ORDER BY clauses.

Index Usage: If the column or columns in the ORDER BY clause include an index, PostgreSQL can perform an index scan to sort data without a sort step. This is beneficial for LIMIT n searches since it fetches the top n sorted rows instead of sorting the entire result set.

Sequential Scan with Explicit Sort: For queries that require to scan a significant area of a database, a sequential scan followed by an explicit sort operation (conducted in memory or on disc) may be faster than an index scan if the index doesn’t fit the data access pattern. Due to disc I/O efficiency.

EXPLAIN Command: This command, along with the ANALYSE and BUFFERS arguments, helps determine how PostgreSQL will process a query and whether an index is handling ORDER BY or requires an expensive sort.

EXPLAIN Command: The CLUSTER command lowers disc I/O for obtaining consecutive data blocks, speeding up index and ORDER BY queries. It physically reorders a table’s disc data to match an index.

Views: Using ORDER BY in a view definition may be meaningless if the data is queried using a different ORDER BY or if the view does not require a precise order.

Conclusion

In conclusion, PostgreSQL ORDER BY clause ensures rows are returned in a predetermined order rather than relying on the database’s physical storage or execution schedule, delivering consistent and predictable query results. Its role in window functions and ordered-set aggregates affects analytical accuracy, while its integration with WHERE, GROUP BY, LIMIT/OFFSET, and set procedures ensures logical processing. Proper indexing, the CLUSTER command, and careful use of EXPLAIN to validate sorting optimisation or explicit operations improve ORDER BY performance. Developers can improve query efficiency, scalability, data integrity, and reproducibility in real-world database systems by studying its behaviour.

Kowsalya
Kowsalya
Hi, I'm Kowsalya a B.Com graduate and currently working as an Author at Govindhtech Solutions. I'm deeply passionate about publishing the latest tech news and tutorials that bringing insightful updates to readers. I enjoy creating step-by-step guides and making complex topics easier to understand for everyone.
Index