Geek University

SQL SELECT LIMIT statement

The SQL SELECT LIMIT statement can be used to return just the first row or a set number of rows of a table. The syntax is:

SELECT column_name1, column_name2, column_name3 FROM table_name LIMIT number;

We will use the Employees table to demonstrate you how to fetch only the first two rows from database table:

+----------------+-----------+-----------+
| employeeNumber | lastName  | firstName |
+----------------+-----------+-----------+
|           1002 | Murphy    | Diane     |
|           1056 | Patterson | Mary      |
|           1076 | Firrelli  | Jeff      |
|           1088 | Patterson | William   |
|           1102 | Bondur    | Gerard    |
|           1143 | Bow       | Anthony   |
|           1165 | Jennings  | Leslie    |
|           1166 | Thompson  | Leslie    |
|           1188 | Firrelli  | Julie     |
|           1216 | Patterson | Steve     |
|           1286 | Tseng     | Foon Yue  |
|           1323 | Vanauf    | George    |
|           1337 | Bondur    | Loui      |
|           1370 | Hernandez | Gerard    |
|           1401 | Castillo  | Pamela    |
|           1501 | Bott      | Larry     |
|           1504 | Jones     | Barry     |
|           1611 | Fixter    | Andy      |
|           1612 | Marsh     | Peter     |
|           1619 | King      | Tom       |
|           1621 | Nishi     | Mami      |
|           1625 | Kato      | Yoshimi   |
|           1702 | Gerard    | Martin    |
+----------------+-----------+-----------+

You can use the LIMIT keyword to limit the top number of entries, as seen here:

SELECT * FROM employees LIMIT 2;

This would produce the following result:

+----------------+-----------+-----------+
| employeeNumber | lastName  | firstName |
+----------------+-----------+-----------+
|           1002 | Murphy    | Diane     |
|           1056 | Patterson | Mary      |
+----------------+-----------+-----------+

Subscribe to our Newsletter

Like us on Facebook

Share on Twitter

top
Developed by Signum Soft