0
0
PostgreSQLquery~10 mins

FETCH FIRST for SQL standard pagination in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - FETCH FIRST for SQL standard pagination
Start Query Execution
Apply WHERE conditions
Apply ORDER BY if present
Apply FETCH FIRST N ROWS ONLY
Return limited result set
End
The query runs filtering and sorting first, then FETCH FIRST limits the number of rows returned.
Execution Sample
PostgreSQL
SELECT * FROM employees ORDER BY id FETCH FIRST 3 ROWS ONLY;
This query returns the first 3 rows from employees table ordered by id.
Execution Table
StepActionIntermediate ResultRows Affected
1Start query executionAll employees table rows10
2Apply ORDER BY idEmployees sorted by id ascending10
3Apply FETCH FIRST 3 ROWS ONLYLimit result to first 3 rows3
4Return result set3 rows returned3
5EndQuery complete3
💡 FETCH FIRST limits output to 3 rows, stopping further row retrieval.
Variable Tracker
VariableStartAfter ORDER BYAfter FETCH FIRSTFinal
rows_in_result1010 (sorted)3 (limited)3
Key Moments - 3 Insights
Why does FETCH FIRST come after ORDER BY?
Because ordering must happen before limiting rows, so the top N rows are the correct ones. See execution_table step 2 and 3.
Does FETCH FIRST change the original table data?
No, FETCH FIRST only limits the output rows of the query, it does not modify the table. See execution_table step 4.
What happens if FETCH FIRST number is larger than total rows?
The query returns all rows without error, just fewer than the FETCH FIRST number. This is implied by step 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many rows are returned after applying FETCH FIRST?
A3
B10
C0
DAll rows
💡 Hint
Check execution_table row 3 and 4 for rows affected after FETCH FIRST.
At which step does the query sort the rows?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table step 2 description about ORDER BY.
If FETCH FIRST was removed, how would the final rows affected change?
AIt would still be 3
BIt would be 0
CIt would be 10
DIt would cause an error
💡 Hint
Refer to variable_tracker rows_in_result after ORDER BY and before FETCH FIRST.
Concept Snapshot
FETCH FIRST N ROWS ONLY limits query output to N rows.
It must come after ORDER BY to get correct top rows.
Does not modify table data, only limits result.
If N > total rows, returns all rows without error.
Syntax example: SELECT * FROM table ORDER BY col FETCH FIRST 5 ROWS ONLY;
Full Transcript
The FETCH FIRST clause in SQL is used to limit the number of rows returned by a query. The query first applies any WHERE filters and then sorts the rows if ORDER BY is present. After sorting, FETCH FIRST N ROWS ONLY restricts the output to the first N rows. This does not change the underlying table data, only the query result. If the number specified in FETCH FIRST is larger than the total rows available, all rows are returned without error. This is useful for pagination or getting a sample of rows. The execution steps show starting with all rows, sorting them, then limiting to the first N rows, and finally returning those rows as the result.