SQL Top Clause sql-top-clause.pdf

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 |
+-----+---------------+--------+-----------+



This article is contributed by Ankur Rastogi on Fri Mar 25 2016 22:00:40 GMT+0530 (IST)

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.