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
- Syntax using Percent
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_NO NAME ADDRESS PHONE Age 1 Ram Delhi XXXXXXXXXX 18 2 RAMESH GURGAON XXXXXXXXXX 18
- To fetch 50 percent of the total records from Student table.
SELECT TOP 50 PERCENT * FROM Student;
Output:
ROLL_NO NAME ADDRESS PHONE Age 1 Ram Delhi XXXXXXXXXX 18 2 RAMESH GURGAON XXXXXXXXXX 18 3 SUJIT ROHTAK XXXXXXXXXX 20
NOTE: To get the same functionality on MySQL and Oracle databases there is a bit of difference in the basic syntax;
- 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:
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
leave a comment
0 Comments