DB2 Tutorial

  • Home
  • Start Here
  • Basics
  • Views
  • Triggers
  • Indexes
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
Home » Db2 Basics » Db2 LIMIT

Db2 LIMIT

Summary: in this tutorial, you will learn how to use the db2 LIMIT clause to limit the number of rows returned by a query.

Introduction to Db2 LIMIT clause

The LIMIT clause allows you to limit the number of rows returned by the query. The LIMIT clause is an extension of the SELECT statement that has the following syntax:

SELECT select_list FROM table_name ORDER BY sort_expression LIMIT n [OFFSET m];

In this syntax:

  • n is the number of rows to be returned.
  • m is the number of rows to skip before returning the n rows.

Another shorter version of LIMIT clause is as follows:

LIMIT m, n;

This syntax means skipping m rows and returning the next n rows from the result set.

A table may store rows in an unspecified order. If you don’t use the ORDER BY clause with the LIMIT clause, the returned rows are also unspecified. Therefore, it is a good practice to always use the ORDER BY clause with the LIMIT clause.

Db2 LIMIT clause examples

We will use the books table from the sample database for the demonstration.

1) Db2 LIMIT OFFSET examples

The following query returns all rows from the books table sorted by titles:

SELECT title, rating FROM books ORDER BY title;
Db2 LIMIT example

To get the first 10 books, you add the LIMIT clause to the above statement:

SELECT title, rating FROM books ORDER BY title LIMIT 10;
Db2 LIMIT top 10 books example

To skip the first 5 books and return the next 10 books, you use the LIMIT OFFSET clause as follows:

SELECT title, rating FROM books ORDER BY title LIMIT 10 OFFSET 5;
Db2 LIMIT OFFSET example

Or in a shorter version:

SELECT title, rating FROM books ORDER BY title LIMIT 5, 10;

2) Using Db2 LIMIT to get top-N rows

The LIMIT clause is useful to get the top-N report e.g., top 10 books that have the highest rating and top 20 books that have the highest number of pages.

This example uses the LIMIT clause to get the top 10 books that have the highest ratings:

SELECT title, rating FROM books ORDER BY rating DESC LIMIT 10;
Db2 LIMIT top 10 books by ratings

In this example:

  • First, sort the books by rating from high to low using the ORDER BY clause
  • Second, pick the first 10 rows from the sorted result set using the LIMIT clause.

In this tutorial, you have learned how to use the Db2 LIMIT clause to restrict the number of rows returned by a query.

  • Was this tutorial helpful ?
  • YesNo
Previous Db2 IN
Next Db2 FETCH

Getting Started

  • What is Db2
  • Installing Db2 Database Server
  • Db2 Sample Database
  • Creating a Db2 Sample Database
  • Connecting to a Db2 Database
  • Interacting with Db2 using SQL Developer

Data Manipulation

  • SELECT
  • ORDER BY
  • WHERE
  • SELECT DISTINCT
  • AND
  • OR
  • BETWEEN
  • LIKE
  • IN
  • LIMIT
  • FETCH
  • Join
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • Self-Join
  • CROSS JOIN
  • GROUP BY
  • Subquery
  • HAVING
  • UNION
  • INTERSECT
  • EXCEPT
  • Common Table Expression or CTE
  • INSERT
  • INSERT Multiple Rows
  • INSERT INTO SELECT
  • UPDATE
  • DELETE

Managing Database Objects

  • CREATE TABLE
  • Identity Columns
  • ALTER COLUMN
  • ADD COLUMN
  • DROP COLUMN
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME TABLE

Db2 Constraints

  • Primary Key
  • Foreign Key
  • DEFAULT
  • NOT NULL
  • CHECK
  • UNIQUE

Db2 Data Types

  • Integer
  • Decimal
  • VARCHAR
  • CHAR
  • DATE
  • TIME
  • TIMESTAMP

Useful Functions & Expressions

  • CAST
  • CASE Expression
  • COALESCE

About db2tutorial.com

The db2tutorial.com website provides you with a comprehensive IBM DB2 tutorial with many practical examples and hands-on sessions.

Recent Tutorials

  • Db2 Functions
  • Db2 RANK
  • Db2 ROW_NUMBER
  • Db2 Window Functions
  • What is Db2

Site Links

  • Home
  • About Us
  • Contact Us
  • Privacy Policy
Copyright © © 2021 by www.db2tutorial.com. All Rights Reserved.