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 MParameters
| Parameter | Description |
|---|---|
orderByItem | A field name to sort by. Separate multiple fields with commas. |
ASC | DESC | Sort direction. Default is ASC (ascending). |
LIMIT N | Maximum number of rows to return. Required when using ORDER BY. |
OFFSET M | Number of rows to skip before returning results. Use with LIMIT for pagination. |
Always include aLIMITclause when usingORDER 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 1000To make the direction explicit:
SELECT nid, brand, price, size FROM phone ORDER BY price ASC LIMIT 1000Sort 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 1000Paginate 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 10Return results without sorting
Omit ORDER BY to return 10 random products without sorting:
SELECT nid, brand, price, size FROM phone LIMIT 10