Monday, 25 July 2016

TOP and LIMIT clauses



Some times you may want to limit the query results to some fixed records. You can do that by using TOP, LIMIT clauses. TOP is in ANSI supported SQL, LIMIT is supported in MySQL.

Note
Not all databases support TOP clauses.
Syntax
SELECT TOP n column-list
FROM table_name
[WHERE condition];

SELECT column-list
FROM table_name
[WHERE condition]
LIMIT n;

mysql> SELECT * FROM employee;
+----+---------+------+------------+---------------------+------------+
| id | name    | age  | salary     | mailId              | city       |
+----+---------+------+------------+---------------------+------------+
|  1 | Hari    |   23 | 13580.2370 | hari@hari.com       | Bangalore  |
|  4 | Manju   |   36 | 96433.5570 | manju@sandesh.com   | Bangalore  |
|  5 | Rakesh  |   26 | 41800.0000 | rakesh@hari.com     | Bangalore  |
|  6 | Sankalp |   38 | 96410.2370 | sankalp@sankalp.com | Chenai     |
|  8 | Prasob  |   37 | 13580.2370 | prasob@sandesh.com  | Trivendram |
+----+---------+------+------------+---------------------+------------+
5 rows in set (0.00 sec)


mysql> SELECT * FROM employee LIMIT 3;
+----+--------+------+------------+-------------------+-----------+
| id | name   | age  | salary     | mailId            | city      |
+----+--------+------+------------+-------------------+-----------+
|  1 | Hari   |   23 | 13580.2370 | hari@hari.com     | Bangalore |
|  4 | Manju  |   36 | 96433.5570 | manju@sandesh.com | Bangalore |
|  5 | Rakesh |   26 | 41800.0000 | rakesh@hari.com   | Bangalore |
+----+--------+------+------------+-------------------+-----------+
3 rows in set (0.00 sec)




Previous                                                 Next                                                 Home

No comments:

Post a Comment