Tutorialspoint

1 Answer
Sharon Christine

To select top 10 records, use LIMIT in MySQL. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> PageNumber text
   -> );
Query OK, 0 rows affected (2.50 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Page-1');
Query OK, 1 row affected (0.46 sec)

mysql> insert into DemoTable values('Page-2');
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable values('Page-3');
Query OK, 1 row affected (0.27 sec)

mysql> insert into DemoTable values('Page-4');
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable values('Page-5');
Query OK, 1 row affected (0.27 sec)

mysql> insert into DemoTable values('Page-6');
Query OK, 1 row affected (1.12 sec)

mysql> insert into DemoTable values('Page-7');
Query OK, 1 row affected (1.21 sec)

mysql> insert into DemoTable values('Page-8');
Query OK, 1 row affected (1.15 sec)

mysql> insert into DemoTable values('Page-9');
Query OK, 1 row affected (0.89 sec)

mysql> insert into DemoTable values('Page-10');
Query OK, 1 row affected (1.20 sec)

mysql> insert into DemoTable values('Page-11');
Query OK, 1 row affected (0.86 sec)

mysql> insert into DemoTable values('Page-12');
Query OK, 1 row affected (0.39 sec)

mysql> insert into DemoTable values('Page-13');
Query OK, 1 row affected (0.39 sec)

mysql> insert into DemoTable values('Page-14');
Query OK, 1 row affected (0.47 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------+
| PageNumber |
+------------+
| Page-1     |
| Page-2     |
| Page-3     |
| Page-4     |
| Page-5     |
| Page-6     |
| Page-7     |
| Page-8     |
| Page-9     |
| Page-10    |
| Page-11    |
| Page-12    |
| Page-13    |
| Page-14    |
+------------+
14 rows in set (0.00 sec)

Following is the query to select top 10 records −

mysql> select *from DemoTable limit 0,10;

This will produce the following output −

+------------+
| PageNumber |
+------------+
| Page-1     |
| Page-2     |
| Page-3     |
| Page-4     |
| Page-5     |
| Page-6     |
| Page-7     | 
| Page-8     |
| Page-9     |
| Page-10    |
+------------+
10 rows in set (0.00 sec)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.