×

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

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.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 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.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.