SQL LIMIT clause in DB2 for i SQL

SQL LIMIT clause in DB2 for i SQL
SQL LIMIT clause in DB2 for i SQL, LIMIT clause example, LIMIT with WHERE clause, LIMIT with ORDER BY clause, sql, sql tutorial, db2 for i sql, ibmi db2
SQL LIMIT clause in DB2 for i SQL

LIMIT clause

The LIMIT clause is used to select the limited number of records.

LIMIT syntax

SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number

Example #1: Select the first 2 records of the STUDENT table:

Refer STUDENT table from here.

Query:

SELECT * FROM Student 
LIMIT 2               

Output:

ROLLNO   NAME                      SUBJECTID
     1   Ankur                           201
     2   Rahul                           202

Example #2: Select the first 2 records of the STUDENT table where ROLLNO is greater than 2:

Refer STUDENT table from here.

Query:

SELECT * FROM Student
where rollno > 2     
LIMIT 2               

Output:

ROLLNO   NAME                      SUBJECTID
     3   Raman                           203
     4   Vimal                           204

Example #3: Select the first 2 records of the STUDENT table order by ROLLNO in descending order:

Refer STUDENT table from here.

Query:

SELECT * FROM Student
order by rollno desc 
LIMIT 2              

Output:

ROLLNO   NAME                      SUBJECTID
     5   Samar                           290
     4   Vimal                           204

Post a Comment

© AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain