TRENDING TOPICS

×

SQL TOP, LIMIT, ROWNUM Clause

The SELECT TOP clause is used to specify the number of records to return from the database. Only MSSQL Access/SQL Server supports the SELECT TOP clause:

LIMIT & ROWNUM Clause

  • MySQL database supports the LIMIT clause to fetch the number of rocords from the database.
  • ORACLE database supports ROWNUM command to specify the number of records.

TOP Clause Syntax

SELECT TOP number/percent column_name(s)
FROM table_name
WHERE [condition];

MySQL LIMIT Syntax

SELECT  column_name(s)
FROM table_name
WHERE [condition]
LIMIT number;

ORACLE ROWNUM Syntax

SELECT  column_name(s)
FROM table_name
WHERE ROWNUM <= number;

Specific Table for SQL TOP Clause

Student Table

A table of student records, the name of table is "Student", and the name of columns are [ROLL_NO, STUDENT_NAME, ADDRESS, MOBILE_NO, AGE, and BRANCH]:

ROLL_NO STUDENT_NAME ADDRESS MOBILE_NO AGE BRANCH
1 Mohit Sharma Delhi XXXXXXXX87 20 IT
2 Pooja Pandit Kolkata XXXXXXXX34 18 CSE
3 Priya Pandey Delhi XXXXXXXX62 19 EEE
4 Mohit Kumar Jha Allahabad U.P XXXXXXXX87 20 ECE
5 Shevani Shree Noida U.P XXXXXXXX51 17 IT
6 Badshah Mumbai XXXXXXXX02 20 Civil
7 Viki Mohan Pune XXXXXXXX72 19 CSE
8 Sona Kaur Indor M.P XXXXXXXX81 22 ME

SQL TOP Clause Example

SELECT TOP 3 *
FROM Student;

Result Look Like

ROLL_NO STUDENT_NAME ADDRESS MOBILE_NO AGE BRANCH
1 Mohit Sharma Delhi XXXXXXXX87 20 IT
2 Pooja Pandit Kolkata XXXXXXXX34 18 CSE
3 Priya Pandey Delhi XXXXXXXX62 19 EEE

SQL LIMIT Clause Example

SELECT  *
FROM Student
LIMIT 2;

Result Look Like

ROLL_NO STUDENT_NAME ADDRESS MOBILE_NO AGE BRANCH
1 Mohit Sharma Delhi XXXXXXXX87 20 IT
2 Pooja Pandit Kolkata XXXXXXXX34 18 CSE

ROWNUM Clause Example

SELECT  *
FROM Student
WHERE ROWNUM <= 3;

Result Look Like

ROLL_NO STUDENT_NAME ADDRESS MOBILE_NO AGE BRANCH
1 Mohit Sharma Delhi XXXXXXXX87 20 IT
2 Pooja Pandit Kolkata XXXXXXXX34 18 CSE
3 Priya Pandey Delhi XXXXXXXX62 19 EEE

© 2021 GDATAMART.COM (All Rights Reserved)