MariaDB LIMIT Clause
Objectives
-
Understanding of MariaDB LIMIT Clause
-
MariaDB LIMIT Clause
-
MariaDB LIMIT Clause with start and end index
-
MariaDB LIMIT Clause with WHERE
Create database and table and insert data if it is not created already
--- Create database if not exist
CREATE DATABASE IF NOT EXISTS mariadb_tutorial;
--- Select the database for further operation
USE mariadb_tutorial;
--- Create table if not exist
CREATE TABLE IF NOT EXISTS person (
id int(12) NOT NULL AUTO_INCREMENT,
first_name varchar(150) NOT NULL,
last_name varchar(150),
email varchar(100),
age int,
income double,
PRIMARY KEY (id)
)
--- Remove old Data
TRUNCATE person;
--- Insert data into table if not exist
INSERT INTO person (first_name, last_name, email, age, income)
VALUES
('Faiyaz', 'Mia', 'faiyaz@pf.local', 1, 5000),
('John', 'Doe', 'john@pf.local', 19, 100),
('Tahsin', NULL, 'tahsin@pf.local', 10, 150),
('Jane', 'Doe', 'jane@gmail.com', 26, 300),
('Rakib', 'Mia', 'rakib@bf.local', 24, 200),
('Sagor', 'Sowrov', 'email10@bf.local', 20, 250),
('Touhid', NULL, 'hmtmcse.com@gmail.com', 30, 500);
Understanding of MariaDB LIMIT Clause
The LIMIT
clause is used to specify the number of records to return. For example your table has 500+ records so if all the records return at a time then it will be difficult to read, LIMIT Clause can narrow the result set.
In programming LIMIT mostly use for pagination of data.
MariaDB LIMIT Clause
Syntax
SELECT * FROM table_name LIMIT number_of_item;
Example
-- From first to total 3 entries
SELECT * FROM person LIMIT 3;
Output
+----+------------+-----------+-----------------+------+--------+
| id | first_name | last_name | email | age | income |
+----+------------+-----------+-----------------+------+--------+
| 1 | Faiyaz | Mia | faiyaz@pf.local | 1 | 5000 |
| 2 | John | Doe | john@pf.local | 19 | 100 |
| 3 | Tahsin | NULL | tahsin@pf.local | 10 | 150 |
+----+------------+-----------+-----------------+------+--------+
3 rows in set (0.001 sec)
MariaDB LIMIT Clause with start and end index
Syntax
SELECT * FROM table_name LIMIT start_index, number_of_item;
Example
-- From number 3 to total 7 entries
SELECT * FROM person LIMIT 3, 7;
Output
+----+------------+-----------+-----------------------+------+--------+
| id | first_name | last_name | email | age | income |
+----+------------+-----------+-----------------------+------+--------+
| 4 | Jane | Doe | jane@gmail.com | 26 | 300 |
| 5 | Rakib | Mia | rakib@bf.local | 24 | 200 |
| 6 | Sagor | Sowrov | email10@bf.local | 20 | 250 |
| 7 | Touhid | NULL | hmtmcse.com@gmail.com | 30 | 500 |
+----+------------+-----------+-----------------------+------+--------+
4 rows in set (0.000 sec)
MariaDB LIMIT Clause with WHERE
Syntax
SELECT * FROM table_name WHERE conditions LIMIT number_of_item;
Example
SELECT * FROM person WHERE age < 20 LIMIT 3;
Output
+----+------------+-----------+-----------------+------+--------+
| id | first_name | last_name | email | age | income |
+----+------------+-----------+-----------------+------+--------+
| 1 | Faiyaz | Mia | faiyaz@pf.local | 1 | 5000 |
| 2 | John | Doe | john@pf.local | 19 | 100 |
| 3 | Tahsin | NULL | tahsin@pf.local | 10 | 150 |
+----+------------+-----------+-----------------+------+--------+
3 rows in set (0.001 sec)