SQL Practice Logo

SQLPractice Online

LIMIT & OFFSET: Functions

Module: SQL Fundamentals

SELECT * FROM products

ORDER BY price DESC

LIMIT 10;

SELECT * FROM products

ORDER BY price DESC

LIMIT 10 OFFSET 20;

-- Page 3, 10 items per page

SELECT * FROM products

ORDER BY product_id

LIMIT 10 OFFSET 20; -- (3-1) * 10 = 20

SELECT * FROM products

ORDER BY price DESC

OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;

LIMIT comes at end of query (after ORDER BY)

OFFSET specifies how many rows to skip

Always use ORDER BY with LIMIT for predictable results

LIMIT without ORDER BY returns arbitrary rows

Cross-database syntax varies (LIMIT vs TOP vs FETCH)

OFFSET formula: (page_number - 1) * page_size

Core references in this topic include WHERE, =, <, >, <=, >=. Learn what each one does, when to use it, and the execution or engine rules that matter.

WHERE

Filters rows before projection and sorting. It decides which rows continue through the query pipeline.

SELECT ... FROM table WHERE condition;

Most performance issues start with a weak WHERE clause or a missing supporting index.

=

Returns rows where the left and right values are exactly equal.

column = value

Use with exact matches. Do not use = NULL.

<, >, <=, >=

Range comparison operators for less-than, greater-than, and inclusive boundary checks.

salary >= 80000

COUNT

Counts rows or non-NULL values depending on the argument.

COUNT(*)

MIN / MAX

Returns the smallest or largest value in the current set.

MIN(order_date), MAX(order_date)

ROWS / RANGE

Defines how a window frame is sliced around the current row.

ROWS BETWEEN 3 PRECEDING AND CURRENT ROW