We use cookies to enhance your experience on the site
CodeWorlds

LIMIT - limiting the number of rows

Sometimes you do not want the whole shelf - only the first few scrolls. That is what LIMIT is for.

LIMIT - only N rows

1SELECT tytul FROM ksiazki LIMIT 5;

This returns the first five books. Combined with ORDER BY, true rankings are born:

1SELECT tytul, cena
2FROM ksiazki
3ORDER BY cena DESC
4LIMIT 3;

This query shows the three most expensive books - we sort descending by price and take only the first three scrolls.

Where does LIMIT stand?

Remember the sacred order: SELECT → FROM → ORDER BY → LIMIT. LIMIT always goes at the very end of the query, after the sorting.

1SELECT imie, nazwisko
2FROM czytelnicy
3ORDER BY nazwisko ASC
4LIMIT 10;

Why limit the result?

  • Preview - you want just a glance at the first rows, not the whole shelf.
  • Rankings - "the top three", "the five newest" are born from ORDER BY + LIMIT.
  • Speed - the Library need not return thousands of scrolls at once.

LIMIT is the scribe's hand that stops the stream of scrolls once the requested number is counted. In the next lesson we will add

OFFSET
to it and learn to paginate.

Go to CodeWorlds