Bird
Raised Fist0
PostgreSQLquery~5 mins

FETCH FIRST for SQL standard pagination in PostgreSQL

Choose your learning style10 modes available
Introduction
FETCH FIRST helps you get a limited number of rows from a big list, making it easier to see just a part of the data at a time.
When you want to show only the top 5 best-selling products from a store.
When you need to display the first 10 recent messages in a chat app.
When you want to preview the first few rows of a large table without loading everything.
When building pages that show data in small chunks, like 20 rows per page.
When you want to limit the number of results returned by a query to save time and resources.
Syntax
PostgreSQL
SELECT column_list
FROM table_name
ORDER BY column_name
FETCH FIRST n ROWS ONLY;
The ORDER BY clause is important to control which rows you get first.
FETCH FIRST n ROWS ONLY limits the output to n rows.
Examples
Gets the top 3 employees with the highest salary.
PostgreSQL
SELECT * FROM employees
ORDER BY salary DESC
FETCH FIRST 3 ROWS ONLY;
Shows the 5 most recently added products.
PostgreSQL
SELECT name FROM products
ORDER BY created_at DESC
FETCH FIRST 5 ROWS ONLY;
Returns any 10 books from the table (order not guaranteed without ORDER BY).
PostgreSQL
SELECT id, title FROM books
FETCH FIRST 10 ROWS ONLY;
Sample Program
This creates a fruits table, adds 5 fruits with prices, then selects the 3 most expensive fruits.
PostgreSQL
CREATE TABLE fruits (id SERIAL PRIMARY KEY, name VARCHAR(20), price NUMERIC);

INSERT INTO fruits (name, price) VALUES
('Apple', 1.20),
('Banana', 0.50),
('Cherry', 2.00),
('Date', 3.00),
('Elderberry', 1.50);

SELECT name, price FROM fruits
ORDER BY price DESC
FETCH FIRST 3 ROWS ONLY;
OutputSuccess
Important Notes
Without ORDER BY, FETCH FIRST returns an arbitrary set of rows, which may vary each time.
FETCH FIRST is part of the SQL standard and works in PostgreSQL 13+ and other modern databases.
You can also use FETCH FIRST n ROWS WITH TIES to include rows that tie on the last value.
Summary
FETCH FIRST limits the number of rows returned by a query.
Always use ORDER BY to control which rows you get first.
Useful for pagination and previewing data.

Practice

(1/5)
1. What does the SQL clause FETCH FIRST 5 ROWS ONLY do in a query?
easy
A. Limits the query result to only the first 5 rows.
B. Skips the first 5 rows and returns the rest.
C. Returns all rows except the first 5.
D. Sorts the rows by the first 5 columns.

Solution

  1. Step 1: Understand FETCH FIRST clause

    The clause FETCH FIRST 5 ROWS ONLY limits the number of rows returned by the query to 5.
  2. Step 2: Effect on query result

    This means only the first 5 rows from the result set will be returned, no more.
  3. Final Answer:

    Limits the query result to only the first 5 rows. -> Option A
  4. Quick Check:

    FETCH FIRST limits rows = A [OK]
Hint: FETCH FIRST means limit rows returned, not skip [OK]
Common Mistakes:
  • Confusing FETCH FIRST with OFFSET
  • Thinking FETCH FIRST sorts rows automatically
  • Assuming FETCH FIRST removes rows instead of limiting
2. Which of the following is the correct syntax to get the first 10 rows from a table named employees in PostgreSQL?
easy
A. SELECT * FROM employees FETCH 10 ROWS;
B. SELECT * FROM employees FETCH FIRST 10 ROWS ONLY;
C. SELECT * FROM employees LIMIT FIRST 10;
D. SELECT * FROM employees TOP 10;

Solution

  1. Step 1: Recall correct FETCH FIRST syntax

    The standard SQL syntax to limit rows is FETCH FIRST n ROWS ONLY. PostgreSQL supports this.
  2. Step 2: Check each option

    SELECT * FROM employees FETCH FIRST 10 ROWS ONLY; uses correct syntax. Options A, B, and D are invalid in PostgreSQL.
  3. Final Answer:

    SELECT * FROM employees FETCH FIRST 10 ROWS ONLY; -> Option B
  4. Quick Check:

    Correct FETCH FIRST syntax = C [OK]
Hint: Use FETCH FIRST n ROWS ONLY for standard limit [OK]
Common Mistakes:
  • Using LIMIT FIRST instead of FETCH FIRST
  • Using TOP keyword which is SQL Server syntax
  • Omitting ROWS ONLY after FETCH FIRST
3. Given the table products with columns id and price, what will this query return?
SELECT id FROM products ORDER BY price DESC FETCH FIRST 3 ROWS ONLY;
medium
A. All products sorted by price descending.
B. The 3 products with the lowest prices.
C. The 3 products with the highest prices.
D. Syntax error due to FETCH FIRST usage.

Solution

  1. Step 1: Analyze ORDER BY clause

    The query orders products by price in descending order, so highest prices come first.
  2. Step 2: Apply FETCH FIRST 3 ROWS ONLY

    This limits the output to only the first 3 rows after sorting, i.e., top 3 highest priced products.
  3. Final Answer:

    The 3 products with the highest prices. -> Option C
  4. Quick Check:

    ORDER BY DESC + FETCH FIRST = top rows [OK]
Hint: ORDER BY DESC + FETCH FIRST = top N highest [OK]
Common Mistakes:
  • Ignoring ORDER BY and thinking it returns random rows
  • Confusing DESC with ASC order
  • Assuming FETCH FIRST causes syntax error
4. Identify the error in this query:
SELECT name FROM customers FETCH FIRST 5 ROWS;
medium
A. Missing ORDER BY clause before FETCH FIRST.
B. No error; query is valid.
C. FETCH FIRST cannot be used without OFFSET.
D. Incorrect FETCH FIRST syntax; missing 'ONLY'.

Solution

  1. Step 1: Check FETCH FIRST syntax

    The correct syntax requires ROWS ONLY after the number, so it should be FETCH FIRST 5 ROWS ONLY.
  2. Step 2: Verify other parts

    ORDER BY is recommended but not mandatory. OFFSET is not required. So the error is missing 'ONLY'.
  3. Final Answer:

    Incorrect FETCH FIRST syntax; missing 'ONLY'. -> Option D
  4. Quick Check:

    FETCH FIRST needs ROWS ONLY [OK]
Hint: FETCH FIRST must end with ROWS ONLY [OK]
Common Mistakes:
  • Omitting 'ONLY' after FETCH FIRST clause
  • Thinking ORDER BY is mandatory with FETCH FIRST
  • Confusing FETCH FIRST with OFFSET usage
5. You want to show page 3 of a product list with 10 items per page, sorted by created_at ascending. Which query correctly uses FETCH FIRST and OFFSET for this pagination?
hard
A. SELECT * FROM products ORDER BY created_at ASC OFFSET 20 ROWS FETCH FIRST 10 ROWS ONLY;
B. SELECT * FROM products ORDER BY created_at ASC FETCH FIRST 10 ROWS OFFSET 20 ROWS ONLY;
C. SELECT * FROM products ORDER BY created_at ASC FETCH FIRST 30 ROWS ONLY;
D. SELECT * FROM products ORDER BY created_at ASC OFFSET 10 ROWS FETCH FIRST 10 ROWS ONLY;

Solution

  1. Step 1: Calculate OFFSET for page 3

    Each page has 10 items, so page 3 starts at row 21. OFFSET is zero-based, so OFFSET = (3-1)*10 = 20.
  2. Step 2: Use FETCH FIRST to limit rows per page

    FETCH FIRST 10 ROWS ONLY limits the result to 10 rows for the page.
  3. Step 3: Check query syntax order

    OFFSET must come before FETCH FIRST. SELECT * FROM products ORDER BY created_at ASC OFFSET 20 ROWS FETCH FIRST 10 ROWS ONLY; correctly orders OFFSET then FETCH FIRST.
  4. Final Answer:

    SELECT * FROM products ORDER BY created_at ASC OFFSET 20 ROWS FETCH FIRST 10 ROWS ONLY; -> Option A
  5. Quick Check:

    OFFSET before FETCH FIRST for pagination [OK]
Hint: OFFSET = (page-1)*size; FETCH FIRST = page size [OK]
Common Mistakes:
  • Swapping OFFSET and FETCH FIRST order
  • Using FETCH FIRST without OFFSET for pages >1
  • Incorrect OFFSET calculation for page number
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard