5. ORDER BY, DISTINCT and Top-N Results
5.1 How to Sort Rows with ORDER BY ASC and DESC
Why. Unsorted results hide the biggest and newest rows.
What. ORDER BY sorts the result. ASC = low to high (default). DESC = high to low.
How. SELECT … ORDER BY amount_inr DESC;
Steps
- Sort amounts DESC.
- Sort dates ASC.
- Remember ORDER BY comes after WHERE.
Examples
Example 1 · dataset: raja_coffee
SELECT order_id, amount_inr
FROM orders
ORDER BY amount_inr DESC
LIMIT 5;Output
| order_id | amount_inr |
|---|---|
| 1016 | 1780.0 |
| 1013 | 1680.0 |
| 1007 | 1080.0 |
| 1004 | 1050.0 |
| 1010 | 930.0 |
Meaning. Five highest amounts.
Ravindra Bagale's Tip
ORDER BY alone does not limit rows — pair with LIMIT for Top-N.
Ravindra Bagale's Tip – मराठी
फक्त ORDER BY रो कमी करत नाही — Top-N साठी LIMIT जोडा.
Ravindra Bagale's Tip – हिंदी
अकेला ORDER BY पंक्तियाँ कम नहीं करता — Top-N के लिए LIMIT जोड़ें।