SQL LESSON 8 OF 32

ORDER BY & LIMIT

Tables have no guaranteed result order without ORDER BY. ASC sorts ascending; DESC descending. LIMIT caps the row count in SQLite. Add a tie-breaker for deterministic results. Other databases use different pagination syntax.

हिन्दी में समझें

ORDER BY के बिना क्रम निश्चित नहीं है। ASC बढ़ता, DESC घटता क्रम है। SQLite में LIMIT पंक्ति सीमा है। बराबर मानों के लिए दूसरा क्रम जोड़ें।

अभ्यास: तीन सबसे सस्ते उत्पाद name और price: price बढ़ता, फिर id क्रम।

Hinglish mein samjhein

ORDER BY ke bina order guaranteed nahi. ASC ascending, DESC descending hai. SQLite LIMIT row count cap karta hai. Ties ke liye second sorting column do.

Practice: 3 cheapest products ke name, price; price ASC phir id order.

A small SQL example

SELECT name, price FROM products ORDER BY price DESC, id LIMIT 2;

Examples use the fictional shop database bundled with the SQL playground: customers, products, orders, order_items and employees. The engine is SQLite; other SQL dialects may differ.

Try this example in the SQL playground →

Practise this idea

Show the three cheapest product names and prices, price ascending then id.

Check answer & explanation
SELECT name, price FROM products ORDER BY price ASC, id LIMIT 3;

Ascending price puts the cheapest items first. id breaks any ties, and LIMIT 3 keeps the first three records.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

Which keyword makes order predictable?

  1. LIMIT
  2. ORDER BY
  3. SELECT *
Check the quiz answer

ORDER BY

Tables have no guaranteed result order without ORDER BY. ASC sorts ascending; DESC descending. LIMIT caps the row count in SQLite. Add a tie-breaker for deterministic results. Other databases use different pagination syntax.

References & corrections

Use the official SQLite documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.