Ravindra BagaleCourses & study guides Track your progress

5. ORDER BY, DISTINCT आणि Top-N निकाल

5.4 ORDER BY + LIMIT ने Top N

का (Why). Top-N हा क्लासिक interview प्रश्न.

काय (What). ORDER BY … DESC LIMIT n त्या क्रमानुसार top n रो देतो.

कसं (How). आधी क्रम, मग LIMIT.

पायऱ्या

  1. Top 3 रकमा.
  2. Top 5 नवीन तारखा.
  3. व्यवसाय अर्थ एका ओळीत.

उदाहरणे

उदाहरण 1 · dataset: raja_coffee

SELECT order_id, city, amount_inr
FROM orders
ORDER BY amount_inr DESC
LIMIT 3;

Output

order_idcityamount_inr
1016Pune1780.0
1013Pune1680.0
1007Nashik1080.0

Meaning. INR रकमेनुसार top 3 कॉफी ऑर्डर्स.

Ravindra Bagale's Tip

Ties: LIMIT 3 may hide a 4th row with the same amount — know that for interviews.