Overview

SQL Top Clause is used to return a subset of the total rows in a table. TOP takes a number or a percent as input and will return those data from the table. TOP Clause is not supported by all RDBMS. But Most of the RDBMS provide same funtionality eiher by using TOP CLause or some other clause.

Syntax

Syntax for TOP Clause is

SELECT TOP NUMBER column_name FROM table_name; 
SELECT TOP PERCENT column_name FROM table_name;

MYSQL Provides this functionality by using LIMIT Clause

SELECT column_name FROM table_name LIMIT NUMBER;

Example

Let us have a look at this with an example

Consider table student_list which contains list of students.

+-----+------------------+--------+-----------+
| sid | name | year | hostel_id |
+-----+------------------+--------+-----------+
|   1 | Ankur Rastogi | First | 4 |
|   2 | Amit Kumar | Second | 8 |
|   3 | Ram Kumar | Second | 8 |
|   4 | Shiv Saxena | First | 6 |
|   5 | Aman Aggarwal | Third | 3 |
|   6 | Nipun Gupta | Fourth | 2 |
|   7 | Arpit Gupta | Third | 1 |
|   8 | Shantanu Rastogi | Second | 3 |
|   9 | Ramesh Kumar | Third | 1 |
|  10 | Neelima Dhingra | Second | NULL |
|  11 | Anu Sibbal | Third | NULL |
+-----+------------------+--------+-----------+
SELECT TOP 2 * FROM student_list;
+-----+---------------+--------+-----------+
| sid | name | year | hostel_id |
+-----+---------------+--------+-----------+
|   1 | Ankur Rastogi | First | 4 |
|   2 | Amit Kumar | Second | 8 |
+-----+---------------+--------+-----------+



If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.



Trending Articles


Incorrect table definition there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

Infix To Postfix Conversion Using Stack in Javascript

How to solve IE/Edge CORS error ?

Web Server vs Application Server Difference Explained

How to setup angular2 in visual studio ide

Binary Search Tree Insert node Implementation in Javascript

How to use ninject dependency injection in mvc

How to Add Comparison Feature in Replace in C-Sharp