mysqlsqlrdbmsclause

Why a LIMIT clause is placed after ORDER clause in MySQL?


SELECT price FROM products LIMIT 1 ORDER BY price; 

this is wrong but

SELECT price FROM products  ORDER BY price LIMIT 1; 

this is okay, why?


Solution

  • Your SELECT statement has two (2) clauses: ORDER BY and LIMIT.

    Per the MySQL SELECT syntax, clauses are defined in a specific sequence as shown in the following image from the MySQL website's SELECT syntax page.

    enter image description here

    The ORDER BY clause is allowed before the LIMIT clause, but not the reverse.