SQL Keywords

SQL NEXT

What is the SQL NEXT keyword used for?

NEXT retrieves the subsequent row or sequence value in operations like cursor fetching, pagination, and sequence generation.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL NEXT: Standard SQL, PostgreSQL (as FETCH NEXT / NEXT FROM and sequence NEXTVAL), SQL Server (FETCH NEXT, NEXT VALUE FOR), Oracle (via FETCH NEXT ROWS and sequence.NEXTVAL syntax), MySQL 8.0+ (FETCH NEXT ROWS only)

SQL NEXT Full Explanation

NEXT is a reserved keyword in the SQL standard that signals the request for the immediately succeeding element in an ordered context. It most commonly appears in two constructs: 1) FETCH NEXT, which advances a cursor or result-set window by a specified number of rows (default 1), and 2) NEXT VALUE FOR, which obtains the next numeric value from a defined sequence object. Because NEXT is positional, its behavior depends on the surrounding clause. For cursors, it moves the cursor pointer forward. For sequences, it increments and returns the sequence counter. NEXT cannot be used standalone; it must be paired with FETCH or VALUE FOR. Misplacing it or omitting required keywords causes syntax errors. Although implementations differ slightly, the semantic purpose remains: obtain the next item in a series.

SQL NEXT Syntax

--Cursor or result-set pagination
FETCH NEXT [ <row_count> ] ROWS ONLY;

--ISO/SQL Server sequence retrieval
NEXT VALUE FOR sequence_name;

SQL NEXT Parameters

Example Queries Using SQL NEXT

--1. Paginate five rows starting at row 11 (offset already applied)
SELECT *
FROM orders
ORDER BY order_date
OFFSET 10 ROWS
FETCH NEXT 5 ROWS ONLY;

--2. Iterate through a cursor one row at a time
DECLARE c CURSOR FOR SELECT id, name FROM customers;
OPEN c;
FETCH NEXT FROM c INTO @id, @name;
CLOSE c;

--3. Get the next invoice number from a sequence
SELECT NEXT VALUE FOR seq_invoice_no AS invoice_no;

Expected Output Using SQL NEXT

  • Returns five rows starting at position 11.
  • Moves cursor c forward one row and loads column values into variables.
  • Returns the next incremental integer from seq_invoice_no, advancing the sequence.

Use Cases with SQL NEXT

  • Paginating web or API responses without loading entire tables
  • Looping through result sets inside stored procedures
  • Generating gapless, incremental identifiers via sequences

Common Mistakes with SQL NEXT

  • Using NEXT without FETCH or VALUE FOR
  • Forgetting ORDER BY when combining OFFSET ... FETCH NEXT, leading to nondeterministic paging
  • Assuming NEXT VALUE FOR is supported in databases that only expose NEXTVAL()

Related Topics

FETCH, OFFSET, FIRST, CURSOR, SEQUENCE, NEXTVAL, LIMIT

First Introduced In

SQL:2008 added OFFSET ... FETCH NEXT; ISO SQL:2011 formalized NEXT VALUE FOR sequences

Frequently Asked Questions

What is the difference between FETCH NEXT and FETCH FIRST?

Both return rows for pagination. FIRST is typically used for the initial page, whereas NEXT is used for subsequent pages but they are functionally identical in most engines.

Does NEXT VALUE FOR lock the sequence?

No. Sequence objects are designed for high concurrency and supply values without row-level locks, though gaps can occur if a transaction rolls back.

Can I use NEXT in a WHERE clause?

No. NEXT must follow FETCH or precede VALUE FOR inside the appropriate clause. Using it elsewhere triggers a syntax error.

What happens if the cursor is already at the end and I call FETCH NEXT?

An empty result is returned and most APIs set the NOT FOUND condition or rowcount to zero, indicating no more rows.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!