SQL ORDER BY Clause

Go back

You can sort your results with ORDER BY:

  • ⬇️ ASC (default, A β†’ Z)
  • ⬆️ DESC (Z β†’ A)
SELECT name [...] ORDER BY name;
SELECT name [...] ORDER BY name ASC;
SELECT name [...] ORDER BY name DESC;
SELECT name [...] ORDER BY name, age DESC;
SELECT name [...] ORDER BY name ASC, age DESC;

You can reference the nth attribute of the select. It's useful when the nth attribute is not a column, but a calculation or something dynamic.

SELECT name [...] ORDER BY 1 DESC;