DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • SQL Commands: A Brief Guide
  • Non-blocking Database Migrations
  • Building an Enterprise CDC Solution
  • 5 Key Postgres Advantages Over MySQL

Trending

  • Why Database Migrations Take Months and How to Speed Them Up
  • Data Quality: A Novel Perspective for 2025
  • How Can Developers Drive Innovation by Combining IoT and AI?
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Do Pagination in Oracle: SQL Query With Example

How to Do Pagination in Oracle: SQL Query With Example

Let's take a look at how to implement pagination when using an Oracle database, along with a quick example.

By 
Javin Paul user avatar
Javin Paul
·
Aug. 01, 18 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
83.8K Views

Join the DZone community and get the full member experience.

Join For Free

Many times, we need a SQL query that returns data page by page i.e. 30 or 40 records at a time, which can be specified as page size. In fact, Database pagination is a common requirement of Java web developers, especially dealing with the largest datasets. In this article, we will see how to query Oracle 10g database for pagination or how to retrieve data using paging from Oracle. Many Java programmer also uses display tag for paging in JSP which supports both internal and external paging. In case of internal paging, all data is loaded into memory in one shot and display tag handles pagination based upon page size but it only suitable for small data where you can afford those many objects in memory.

If you have hundreds of row to display than its best to use external pagination by asking the database to do pagination. In pagination, ordering is another important aspect which can not be missed.

It's virtually impossible to sort large collection in Java using limited memory available to Java program, sorting data in a database using ORDER BY clause itself is a good solution while doing paging in a web application.

Pagination SQL Query in Oracle 10g Database With Example:

In database paging, we only query data that we are required to show or maybe up to 3 pages just to prefetch some data in advance for performance reason.

Thankfully, Oracle database provides a convenient method row_number() that can be used to provide a unique row number to each row in result set. See Oracle PL/SQL Fundamentals — Part 1 to learn more about row_number and other pagination methods in Oracle database.

By including row_number() in the query, you can produce a result set that is numbered, and then its just a job to retrieve data from specified indexes or pages. Here is an example of a pagination query in Oracle 12c database:

SELECT * FROM (
    SELECT
      ord.*,
      row_number() over (ORDER BY ord.order_id ASC) line_number
    FROM Orders ord

  ) WHERE line_number BETWEEN 0 AND 5  ORDER BY line_number;

This will print the result of a query including an additional column called line_number, which will automatically be populated by Oracle because of row_number() function.

You can also see Oracle Database 12c Fundamentals to learn more about the row_number function of Oracle 12c database, which is a free online course from Pluralsight to learn Oracle database in detail. There are two parts of this tutorial, both are freely available once you signup for the 10-day free trial on the Pluralsight website.

By using this line_number column, now we can get the result page by page based on the size of the page or more specifically from one row to another like from 1 to 30 or 5th to 30, since you can pass starting row and end row from Java program to Oracle database.

Though I know MySQL database has inbuilt paging support using the LIMIT keyword, this row_number() function of Oracle is equally useful for paging in Oracle database.

I have also found that SQL Server also supports row_number() function, which can be used to get data page by page in SQL Server. By the way, if you are preparing for Oracle Certification e.g. 1Z0-066 to become a certified Oracle Database administrator, then you can check out David Mayer's free 1Z0-066 dumps for reference.

Database sql Data (computing) MySQL

Published at DZone with permission of Javin Paul, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • SQL Commands: A Brief Guide
  • Non-blocking Database Migrations
  • Building an Enterprise CDC Solution
  • 5 Key Postgres Advantages Over MySQL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!