ORDER BY, LIMIT, and DISTINCT
The question "what are the three most expensive orders?" shows up in interviews in every possible form. Answering it takes three tools: ORDER BY to sort, LIMIT to cut after N rows, and DISTINCT to remove duplicates. We keep using the same two shop tables.
ORDER BY arranges the result, LIMIT keeps only its start, and DISTINCT keeps each different row once.
- ORDER BY
- A clause that sorts the result rows by one or more columns.
- Sort direction
- ASC from smallest to largest (the default), DESC from largest to smallest.
- LIMIT
- A clause that returns only the first N rows of the result.
- DISTINCT
- A keyword after SELECT that removes duplicate rows from the result.