Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SQL
SQL
0/12 completed
Beginner
What is a database?SELECT: reading dataFiltering with WHERESorting and limiting results
Intermediate
Aggregates and GROUP BYJOINs: combining tablesINSERT, UPDATE and DELETECreating tables and constraints
Advanced
Subqueries and CTEsWindow functions
Pro
Indexes and transactionsDatabase design and normalization
Tracks / SQL / Beginner

Sorting and limiting results

Without ORDER BY, a database may return rows in any order, and that order can change. If order matters, always sort explicitly.

ORDER BY

SQL · editable
SQL · editable

Sorting by several columns

Rows are sorted by the first column, and ties are broken by the next.

SQL · editable

You can also sort by an alias or an expression:

SQL · editable

LIMIT and OFFSET

LIMIT caps how many rows come back. It's perfect for "top N" queries.

SQL · editable

OFFSET skips rows. Together they give you pagination: page 2 with 3 items per page skips 3.

SQL · editable

Pro note: OFFSET gets slow on big tables because the database still reads all the skipped rows. Large apps use keyset pagination instead: WHERE price < :last_seen_price ORDER BY price DESC LIMIT 20.

Different databases spell this slightly differently: SQL Server uses TOP 3 or OFFSET … FETCH, while PostgreSQL, MySQL and SQLite use LIMIT.

Order of clauses

Clauses must be written in this order:

sql
SELECT   columns
FROM     table
WHERE    row filter
ORDER BY sort columns
LIMIT    n;

Practice

  1. Show the 5 most recent orders (newest order_date first).
  2. Show the cheapest Electronics product.
SQL · editable
✎ Check your understanding

1.Without ORDER BY, what order are rows returned in?

2.For page 3 with 10 rows per page, what do you use?

← Filtering with WHERE