×

SQL Top Clause

Top Clause:

It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored in the database.

SELECT TOP clause returns the limited number of rows as output.

All databases do not support the TOP clause, Hence the Oracle database uses the ROWNUM clause instead of the TOP clause. The Mysql database uses the LIMIT clause instead of the TOP clause.

The syntax for TOP Clause:

Select TOP number | Percent column1, column2, column3, ……, column from Table_name where ( condition);

Example:

Student Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

1.Display the first three rows of the Student table with sid and sname

 Code:

Select TOP 3 sid, sname from Student;

Output:

sidsname
1Abhinav
2Ramya
3Preetham

2. Display the Student table using percent i.e, 50 percent with all Student details.

Code:

Select TOP 50 PERCENT * from Student;

Output:

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532

3. Display the first eight rows of table whose gender is Female with student details:

Code:

Select TOP 8 * from Student where sgender = ‘Female’;

Output:

sidsnamesagesgenderPhonenumber
2Ramya24Female6687654634
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839

ROWNUM in Oracle:

It returns the first ‘n’ number of rows as output. This  clause is used to retrieve the required number of rows when there are thousands of records stored in the database.

The syntax for ROWNUM:

SELECT column1, column2, column3, ……, column from Table_name where ROWNUM <= value;

Example:

Student Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

1.Display the first three rows of the Student table with sid and sname

  Code:

SELECT  sid, sname from Student where ROWNUM <= 3;

 Output:

sidsname
1Abhinav
2Ramya
3Preetham

2. Display the Student table using top 5 rows with all Student details.

Code:

SELECT * from Student where ROWNUM <= 5;

Output:

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532

3. Display the first eight rows of the table whose gender is Female with student details:

Code:

SELECT  * from (SELECT * from Student where ROWNUM <= 8) where sgender = ‘Female’;

Output:

sidsnamesagesgenderPhonenumber
2Ramya24Female6687654634
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839

LIMIT in Mysql:

It returns the first ‘n’ number of rows as output. This clause is used to retrieve the required number of rows when there are thousands of records stored in the database.

The syntax for LIMIT clause:

SELECT column1, column2, column3, ……, column from Table_name LIMIT value;

Example:

Student Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

1.Display the first three rows of the Student table with sid and sname

  Code:

 SELECT  sid, sname from Student LIMIT 3;

 Output:

sidsname
1Abhinav
2Ramya
3Preetham

2. Display the Student table using  with all Student details of top 5 rows.

Code:

SELECT  * from Student LIMIT 5;

Output:

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532

3. Display the first eight rows of table whose gender is Female with student details:

Code:

SELECT * from ( SELECT * from Student LIMIT 8) where sgender = ‘Female’;

Output:

sidsnamesagesgenderPhonenumber
2Ramya24Female6687654634
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839

Related Topics

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

WEB SQL

What is Web SQL? In SQL we can create databases, read the data in the databases, insert the records into the databases, and delete the records from the databases. For storing...

4 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

SQL INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.