All Products
Search
Document Center

OpenSearch:ORDER BY

Last Updated:Apr 01, 2026

Use ORDER BY to sort query results by one or more fields. Always pair ORDER BY with a LIMIT clause—sorting without a row limit degrades query performance significantly.

Requirements

OpenSearch Retrieval Engine Edition with HA3 version V3.7.0 or later.

Syntax

SELECT [ DISTINCT ]
  { projectItem [, projectItem ]* }
FROM tableExpression
  ORDER BY { orderByItem [ASC|DESC] [, orderByItem [ASC|DESC] ]* }
  LIMIT N
  OFFSET M

Parameters

ParameterDescription
orderByItemA field name to sort by. Separate multiple fields with commas.
ASC | DESCSort direction. Default is ASC (ascending).
LIMIT NMaximum number of rows to return. Required when using ORDER BY.
OFFSET MNumber of rows to skip before returning results. Use with LIMIT for pagination.
Always include a LIMIT clause when using ORDER BY. Sorting without a row limit degrades query performance significantly.

Examples

Sort by a single field

Sort results by price in ascending order (default):

SELECT nid, brand, price, size FROM phone ORDER BY price LIMIT 1000

To make the direction explicit:

SELECT nid, brand, price, size FROM phone ORDER BY price ASC LIMIT 1000

Sort by multiple fields

Sort by size descending, then by price descending within each size group:

SELECT nid, brand, price, size FROM phone ORDER BY size DESC, price DESC LIMIT 1000

Paginate sorted results

Retrieve records 11–20 in a price-sorted result set using LIMIT and OFFSET:

SELECT nid, brand, price, size FROM phone ORDER BY price DESC LIMIT 10 OFFSET 10

Return results without sorting

Omit ORDER BY to return 10 random products without sorting:

SELECT nid, brand, price, size FROM phone LIMIT 10