SQL | SELECT TOP Clause - GeeksforGeeks

09 Jan, 2019
SELECT TOP clause is used to fetch limited number of rows from a database. This clause is very useful while dealing with large databases.
Basic Syntax:SELECT TOP value column1,column2 FROM table_name;
value: number of rows to return from top
column1 , column2: fields in the table
table_name: name of table

SELECT TOP value column1,column2 FROM table_name;
value: number of rows to return from top
column1 , column2: fields in the table
table_name: name of table

Syntax using PercentSELECT TOP value PERCENT column1,column2 FROM table_name;
value: percentage of number of rows to return from top
column1 , column2: fields in the table
table_name: name of table

SELECT TOP value PERCENT column1,column2 FROM table_name;
value: percentage of number of rows to return from top
column1 , column2: fields in the table
table_name: name of table

Queries
To fetch first two data set from Student table.SELECT TOP 2 * FROM Student; 
Output:ROLL_NONAMEADDRESSPHONEAge1RamDelhiXXXXXXXXXX182RAMESHGURGAONXXXXXXXXXX18
To fetch first two data set from Student table.SELECT TOP 2 * FROM Student; 
Output:ROLL_NONAMEADDRESSPHONEAge1RamDelhiXXXXXXXXXX182RAMESHGURGAONXXXXXXXXXX18
To fetch first two data set from Student table.SELECT TOP 2 * FROM Student; 
Output:ROLL_NONAMEADDRESSPHONEAge1RamDelhiXXXXXXXXXX182RAMESHGURGAONXXXXXXXXXX18
To fetch first two data set from Student table.SELECT TOP 2 * FROM Student; 
Output:ROLL_NONAMEADDRESSPHONEAge1RamDelhiXXXXXXXXXX182RAMESHGURGAONXXXXXXXXXX18
SELECT TOP 2 * FROM Student; 

Output:
 
To fetch 50 percent of the total records from Student table.SELECT TOP 50 PERCENT * FROM Student; 
Output:ROLL_NONAMEADDRESSPHONEAge1RamDelhiXXXXXXXXXX182RAMESHGURGAONXXXXXXXXXX183SUJITROHTAKXXXXXXXXXX20
To fetch 50 percent of the total records from Student table.SELECT TOP 50 PERCENT * FROM Student; 
Output:ROLL_NONAMEADDRESSPHONEAge1RamDelhiXXXXXXXXXX182RAMESHGURGAONXXXXXXXXXX183SUJITROHTAKXXXXXXXXXX20
To fetch 50 percent of the total records from Student table.SELECT TOP 50 PERCENT * FROM Student; 
Output:ROLL_NONAMEADDRESSPHONEAge1RamDelhiXXXXXXXXXX182RAMESHGURGAONXXXXXXXXXX183SUJITROHTAKXXXXXXXXXX20
SELECT TOP 50 PERCENT * FROM Student; 

Output:
NOTE: To get the same functionality on MySQL and Oracle databases there is a bit of difference in the basic syntax;
Equivalent Syntaxes are as follows:For MySQL databases:SELECT column1,column2 FROM table_name LIMIT value;
column1 , column2: fields int the table
table_name: name of table
value: number of rows to return from top
For Oracle databases:SELECT column1,column2 FROM table_name WHERE ROWNUM <= value;
column1 , column2: fields int the table
table_name: name of table
value: number of rows to return from top

Equivalent Syntaxes are as follows:For MySQL databases:SELECT column1,column2 FROM table_name LIMIT value;
column1 , column2: fields int the table
table_name: name of table
value: number of rows to return from top
For Oracle databases:SELECT column1,column2 FROM table_name WHERE ROWNUM <= value;
column1 , column2: fields int the table
table_name: name of table
value: number of rows to return from top

For MySQL databases:SELECT column1,column2 FROM table_name LIMIT value;
column1 , column2: fields int the table
table_name: name of table
value: number of rows to return from top

SELECT column1,column2 FROM table_name LIMIT value;
column1 , column2: fields int the table
table_name: name of table
value: number of rows to return from top

For Oracle databases:SELECT column1,column2 FROM table_name WHERE ROWNUM <= value;
column1 , column2: fields int the table
table_name: name of table
value: number of rows to return from top

SELECT column1,column2 FROM table_name WHERE ROWNUM <= value;
column1 , column2: fields int the table
table_name: name of table
value: number of rows to return from top

This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
SQL-Clauses-Operators
Articles
DBMS
SQL
DBMS
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Time Complexity and Space Complexity
Docker - COPY Instruction
Time complexities of different data structures
Difference between Class and Object
Difference between Min Heap and Max Heap
SQL | WITH clause
ACID Properties in DBMS
SQL query to find second highest salary?
Normal Forms in DBMS
Introduction of B-Tree