Updated on 2025-09-05 GMT+08:00

OrderBy & Limit

OrderBy

  • Function

    This function sorts result sets based on one or more columns.

  • Syntax Format
    1
    2
    3
    4
    5
    6
    SELECT [ ALL | DISTINCT ]
      { * | projectItem [, projectItem ]* }
      FROM tableExpression
      [ WHERE booleanExpression ]
      [ GROUP BY { groupItem [, groupItem ]* } ]
      [ ORDER BY Itemtime|Item [ASC | DESC] ]
    
  • Precautions

    By default, ORDER BY sorts data in ascending order. ASC: indicates the ascending order, and DESC the descending order.

  • Example

    Sort data in ascending order on the time attribute.

    1
    2
    3
    SELECT *
    FROM Orders
    ORDER BY orderTime;
    

Limit

  • Function

    This clause is used to constrain the number of rows returned.

  • Precautions

    This clause is used in conjunction with ORDER BY to ensure that the results are deterministic.

  • Example
    1
    2
    3
    4
    SELECT *
    FROM Orders
    ORDER BY orderTime
    LIMIT 3;