Back to Chapters
Chapter Progress: 0 %0 / 6 Lessons Read
β€Ήβ€Ί

1. SQL basics: ask a table a question

1.3 ORDER BY and LIMIT: sort and take the top rows

Highlights & notes

Select lesson text to highlight it, or tap Add note.

    Bookmarks

    Bookmark a lesson to see it here.

      ORDER BY sorts the result. It sorts small to large (ASC) unless you write DESC for large to small. LIMIT 3 then keeps only the first 3 rows.

      • ORDER BY amount DESC β†’ biggest amount first
      • ORDER BY city, name β†’ sort by city, then by name inside each city
      • ORDER BY ... LIMIT 1 β†’ the single top row

      You write a query in one order, but the database runs it in another: FROM β†’ WHERE β†’ SELECT β†’ ORDER BY β†’ LIMIT. Knowing this order explains most SQL errors.

      pandas vs SQL

      • df.sort_values("amount", ascending=False) β†’ ORDER BY amount DESC
      • df.head(3) β†’ LIMIT 3
      Must Know:

      Without ORDER BY, a database may return rows in any order, and the order can change between runs. If the order matters, always write ORDER BY.

      How it fits together

      1. Filter rows (WHERE)
      2. Sort rows (ORDER BY)
      3. Keep the top rows (LIMIT)

      Watch the query run

      The line that lights up shows the order the database really runs the query in.

      SQL

      SELECT product, amountFROM ordersWHERE status = 'delivered'ORDER BY amount DESCLIMIT 3;

      Rows at this step

      product | amount
      --------+-------
      Laptop  | 55000
      Laptop  | 52000
      Monitor | 11500

      Practice in SQL

      Show the id and order_date of the 3 newest orders, newest first.

      Tables you can query: customers, orders
      customers
      id | name  | city
      ---+-------+------
      1  | Asha  | Noida
      2  | Ravi  | Delhi
      3  | Meera | Pune
      4  | Kabir | Noida
      5  | Zoya  | NULL
      
      orders
      id  | customer_id | product  | amount | status    | order_date
      ----+-------------+----------+--------+-----------+-----------
      101 | 1           | Laptop   | 55000  | delivered | 2026-01-05
      102 | 2           | Mouse    | 700    | delivered | 2026-01-06
      103 | 1           | Keyboard | 1500   | delivered | 2026-01-09
      104 | 3           | Monitor  | 12000  | cancelled | 2026-01-12
      105 | 2           | Laptop   | 52000  | delivered | 2026-01-15
      106 | 4           | Mouse    | 650    | pending   | 2026-01-18
      107 | 1           | Monitor  | 11500  | delivered | 2026-01-20
      108 | 3           | Keyboard | NULL   | pending   | 2026-01-22
      Expected output
      id  | order_date
      ----+-----------
      108 | 2026-01-22
      107 | 2026-01-20
      106 | 2026-01-18

      Reference solution
      SELECT id, order_date
      FROM orders
      ORDER BY order_date DESC
      LIMIT 3;

      Question 1 Β· Easy

      Which keyword sorts from largest to smallest?

      Question 2 Β· Medium

      Which part of a query does the database run first?

      Question 3 Β· Hard

      A query has LIMIT 5 but no ORDER BY. What do you get?

      Key takeaways

      ORDER BY sorts and LIMIT keeps the top rows. The run order is FROM, WHERE, SELECT, ORDER BY, LIMIT.

      Lesson 3 of 24

      Save your progress

      Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.

      Start free Log in
      PreviousORDER BY and LIMIT: sort and take the top rows
      Save your progress free for 1 month. Start free Β· Log in