SQL and databases

Stage 2 · Lesson 5 of 8

Limiting rows: LIMIT, TOP and OFFSET FETCH

Querying one table

Most queries in real apps return one page of results, not every row. and MySQL use with OFFSET (how many rows to skip). uses TOP (n), or the standard OFFSET ... ROWS FETCH NEXT ... ROWS ONLY form, which needs .

PostgreSQL also accepts the standard FETCH FIRST form. This is the first difference you will meet, where databases spell the same idea differently. Learn to recognise every form, even though you only write your own.

Always pair a limit with ORDER BY, or the rows you get are arbitrary. OFFSET works for early pages but gets slower as the number grows, because the database still reads the rows it skips. The performance stage shows a better method.

Common trapTOP or LIMIT without ORDER BY in a "latest ten" query.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • CoursePaid
    Grokking SQL for Tech Interviews

    Design Gurus · Intermediate · Subscription

    The basics, order of execution and functions chapters, each followed by graded problems

  • CourseFree
    SQLBolt

    SQLBolt · Beginner

    Free: Lessons 1 to 4, 8, 9 and 12 cover SELECT, filtering, sorting, NULLs, expressions and query order

Practise

Keep handy

  • The SELECT statement page and the string and date function chapters

  • Its SQL pages give quick examples and note where MySQL and SQL Server syntax differ

Also worth your time

Mark this stage as not done?