All Products
Search
Document Center

OpenSearch:ORDER BY

Last Updated:Apr 09, 2025

You can use ORDER BY to sort one or more fields. The default value is the ascending order (ASC). When you sort data, you must use a LIMIT clause to improve the sorting performance.

Syntax

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

Examples

  1. Sort data in the simplest manner:

SELECT nid, brand, price, size FROM phone ORDER BY price LIMIT 1000
  1. Sort data in ascending order:

SELECT nid, brand, price, size FROM phone ORDER BY price ASC LIMIT 1000
  1. Sort data based on multiple fields:

SELECT nid, brand, price, size FROM phone ORDER BY size DESC, price DESC LIMIT 1000
  1. Return the records of the eleventh to twentieth prices in the price sorting result:

SELECT nid, brand, price, size FROM phone ORDER BY price DESC LIMIT 10 OFFSET 10
  1. Return the data of 10 random products without sorting data:

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