Quick revision
The short answer
A Page contains the requested rows plus the total number of elements and pages, so Spring Data runs an additional count query to build it. A Slice contains the rows and only knows whether a next slice exists, which Spring Data finds out by fetching one extra row, so there is no count query. A List with a Pageable parameter applies the limit and offset and returns just the rows, with no metadata. Slice is the better choice for infinite scroll and large tables where counting is costly.
What to remember
In this answer, we saw that Page, Slice and List all apply limit and offset, but Page runs an extra count query to report totals, Slice fetches one extra row to know whether more data exists, and List returns only the rows. The count can cost more than the content on large tables, so Slice is the better choice for infinite scroll and large data sets, while Page belongs where the screen really shows totals.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A Page contains the requested rows plus the total number of elements and pages, so Spring Data runs an additional count query to build it. A Slice contains the rows and only knows whether a next slice exists, which Spring Data finds out by fetching one extra row, so there is no count query. A List with a Pageable parameter applies the limit and offset and returns just the rows, with no metadata. Slice is the better choice for infinite scroll and large tables where counting is costly.
Why this question is asked
Paging looks like a solved problem: pass a Pageable, get a page back. But the return type we choose decides how many queries run per request, and on a table with fifty million rows, the difference between Page and Slice can be the difference between a 5 millisecond endpoint and a 2 second one. Interviewers ask this to see whether we know what each type costs and can match it to what the user interface actually needs. In this answer, we will recap Pageable, look at the SQL behind each return type, see why the count can be expensive, and decide which type fits which screen.
1A Quick Recap of Pageable
A Pageable describes the slice of data we want: a page number starting at 0, a page size and an optional sort. PageRequest.of(2, 20, Sort.by("createdAt").descending()) means the third page of 20 rows, newest first. Spring Data turns this into an offset of 40 and a limit of 20 on the query.
Any repository query method can take a Pageable as its last parameter. What changes between Page, Slice and List is what else Spring Data does to build the result.
2The Same Method With Three Return Types
Here are three versions of one query, and the SQL each runs for page 2 of size 20.
| Return type | Queries per call | Knows | Good for |
|---|---|---|---|
Page<T> | Content + count | Total rows, total pages, has next | Numbered pages with totals |
Slice<T> | Content (size + 1 rows) | Has next, has previous | Infinite scroll, next buttons |
List<T> | Content only | Nothing beyond the rows | Fixed-size lists, internal use |
Notice the small difference in the Slice query: it asks for 21 rows, not 20. If the 21st row comes back, Spring Data knows there is a next slice, drops that row and returns the first 20 with hasNext() true. That one extra row is far cheaper than counting everything. The exact limit syntax depends on the database dialect.
3Why the Count Can Be Expensive
The count query must find every matching row to count it. For an author with 1,873 posts and an index on author_id, that is cheap. For a query like all orders in the last year on a table of fifty million rows, the count may read millions of index entries or table rows on every single request, even though the user only sees 20 rows.
It gets worse with filters that use several columns or joins, and in PostgreSQL in particular, count(*) cannot simply read a stored number; it has to check row visibility, which often means touching the table. So the count can easily take longer than the content query, and it runs on every page, including page 1, where the user may never look at the total at all.
Spring Data does try to avoid it in one case: if the content query returns fewer rows than the page size on the first page, it knows the total without counting. But for any page that is full, the count runs.
4Matching the Type to the Screen
The choice comes from the user interface. If the screen shows page numbers, 1 2 3 ... 94, or a total such as 1,873 results, it needs a Page, and we should make sure the count query is cheap, with the right index or a hand-written countQuery. If the screen is an infinite scroll or has only a next button, it needs only hasNext(), and a Slice gives that without a count. If the code needs a fixed number of rows, such as the top 10 posts for a widget, or processes batches internally, a List with a Pageable, or a Top10 method, is enough.
- Does the screen show totals or page numbers?YesPage; make the count cheapNoNext question
- Does it need to know if more data exists?YesSliceNoList with Pageable or Top N
- Check deep offsetsLarge page numbers are slow for all three
3 steps in order. Play them, or step through them one at a time.
5A Problem All Three Share: Deep Offsets
All three types use offset paging. To serve page 5,000 of size 20, the database finds and discards 100,000 rows before returning 20, so deep pages get slower and slower whatever the return type. Slice fixes the count cost, not the offset cost. For feeds and large tables where users scroll deep, keyset pagination, which asks for rows after the last seen sort value, avoids offsets entirely; Spring Data supports it through the Window and ScrollPosition API.
6Follow-up questions
Can I get a Page without the count when I know the total some other way?
Yes, with a custom implementation that builds a PageImpl from the content and a total from elsewhere, such as a cached or approximate count. Spring Data's PageableExecutionUtils.getPage also skips the count when it can deduce the total from the content.
Does a Slice support sorting?
Yes. The Pageable's sort is applied to the content query as usual. Only the count is skipped.
What happens if rows are inserted between two page requests?
With offset paging, rows shift: an insert at the top pushes a row from page 1 onto page 2, so the user sees it twice, and a delete can make a row skipped. Keyset pagination avoids that because each request starts after a specific row rather than at a position.
7Conclusion
In this answer, we saw that Page, Slice and List all apply limit and offset, but Page runs an extra count query to report totals, Slice fetches one extra row to know whether more data exists, and List returns only the rows. The count can cost more than the content on large tables, so Slice is the better choice for infinite scroll and large data sets, while Page belongs where the screen really shows totals.
Open with
All three take a Pageable and apply limit and offset to the query, so the content is the same. The difference is what else happens. A Page has the total number of elements and pages, so Spring Data runs a second query, a count with the same filter.
Then, if they want more
A Slice only knows whether there is a next slice, and it finds that out cleverly: it asks for page size plus one rows, and if the extra row comes back, hasNext is true and the row is dropped. So there is no count query. A List with a Pageable just returns the rows with no metadata at all.
The reason this matters is that the count can be the most expensive part. On a large table with a broad filter, counting every matching row on every request can take longer than fetching the 20 rows, and in PostgreSQL count has to check row visibility, so it is rarely free. So I choose by what the screen needs.
If it shows page numbers or a total, I use Page and make sure the count is cheap, with an index or a hand-written count query. For infinite scroll or a next button, I use Slice. For a fixed-size list or internal batching, a List or a Top N method is enough.
One thing all three share is offset paging, so deep pages are slow regardless; for feeds that users scroll deeply, I would use keyset pagination with Window and ScrollPosition.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the difference between Page, Slice and List as return types for a paged...": https://scrivio-interview-library.netlify.app/databases/spring-data-jpa/f56e9a7614/