← Back to lessons Beginner

ORDER BY and LIMIT

Sort results to produce a useful output.

Beginner SQL syntax Finance and accounting scenario Related exercises

Objective

Organize data in a logical order: descending amount, ascending date or alphabetical customer name.

Exercise context

Sorting invoices by amount, customers by revenue or products by margin produces clear results and highlights key values.

Key concepts

  • ORDER BY sorts the rows returned by a query.
  • ASC means ascending order.
  • DESC means descending order.
  • Sorting can use an existing column or a calculated alias.
  • LIMIT restricts the number of returned rows.

Syntax pattern

SELECT colonne1, colonne2
FROM table
ORDER BY colonne DESC
LIMIT 10;

Applied example

SELECT id_facture, date_facture, montant_ttc
FROM factures
ORDER BY montant_ttc DESC;

Common mistakes

  • Placing ORDER BY before WHERE.
  • Sorting with a misspelled alias.
  • Forgetting DESC when the largest amount should come first.

Recommended exercises