Use ORDER BY to sort and LIMIT to return only a few rows.
Use ORDER BY to sort and LIMIT to return only a few rows. Read the query line by line and identify the input table, the transformation, and the shape of the result before running it.
SELECT order_id, amount FROM orders ORDER BY amount DESC, order_id ASC LIMIT 3;
Before running the example, predict the output columns and whether the query returns one row, many rows, or possibly no rows.
In your own words, explain the main SQL feature in today’s lesson and why it answers the question.
Recall prompt: Sort amount DESC, then order_id ASC; apply LIMIT 3. Close this page section mentally and explain the query from memory before proceeding.
Return the three largest orders, breaking ties by order_id ascending.
Sort amount DESC, then order_id ASC; apply LIMIT 3.