How to select first 10 elements from a MySQL database?

MySQLMySQLi Database

To select first 10 elements from a database using SQL ORDER BY clause with LIMIT 10.

The syntax is as follows

SELECT *FROM yourTableName ORDER BY yourIdColumnName LIMIT 10;

To understand the above syntax, let us create a table. The query to create a table is as follows

mysql> create table Clients
   - > (
   - > Client_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   - > ClientName varchar(20)
   - > );
Query OK, 0 rows affected (0.51 sec)

Insert some records in the table using insert command.

The query is as follows

mysql> insert into Clients(ClientName) values('Larry');
Query OK, 1 row affected (0.09 sec)
mysql> insert into Clients(ClientName) values('Sam');
Query OK, 1 row affected (0.19 sec)
mysql> insert into Clients(ClientName) values('Bob');
Query OK, 1 row affected (0.18 sec)
mysql> insert into Clients(ClientName) values('David');
Query OK, 1 row affected (0.15 sec)
mysql> insert into Clients(ClientName) values('John');
Query OK, 1 row affected (0.17 sec)
mysql> insert into Clients(ClientName) values('James');
Query OK, 1 row affected (0.14 sec)
mysql> insert into Clients(ClientName) values('Robert');
Query OK, 1 row affected (0.11 sec)
mysql> insert into Clients(ClientName) values('Carol');
Query OK, 1 row affected (0.15 sec)
mysql> insert into Clients(ClientName) values('Mike');
Query OK, 1 row affected (0.12 sec)
mysql> insert into Clients(ClientName) values('Maxwell');
Query OK, 1 row affected (0.27 sec)
mysql> insert into Clients(ClientName) values('Chris');
Query OK, 1 row affected (0.23 sec)
mysql> insert into Clients(ClientName) values('Ramit');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement.

The query is as follows

mysql> select *from Clients;

The following is the output

+-----------+------------+
| Client_Id | ClientName |
+-----------+------------+
|         1 | Larry      |
|         2 | Sam        |
|         3 | Bob        |
|         4 | David      |
|         5 | John       |
|         6 | James      |
|         7 | Robert     |
|         8 | Carol      |
|         9 | Mike       |
|        10 | Maxwell    |
|        11 | Chris      |
|        12 | Ramit      |
+-----------+------------+
12 rows in set (0.00 sec)

Here is the query to select first 10 elements

mysql> select *from Clients ORDER BY Client_Id LIMIT 10;

The following is the output

+-----------+------------+
| Client_Id | ClientName |
+-----------+------------+
|         1 | Larry      |
|         2 | Sam        |
|         3 | Bob        |
|         4 | David      |
|         5 | John       |
|         6 | James      |
|         7 | Robert     |
|         8 | Carol      |
|         9 | Mike       |
|        10 | Maxwell    |
+-----------+------------+
10 rows in set (0.00 sec)

Here is the alternate query to select first 10 elements.

The query is as follows

mysql> select *from Clients limit 0,10;

The following is the output

+-----------+------------+
| Client_Id | ClientName |
+-----------+------------+
|         1 | Larry      | 
|         2 | Sam        |
|         3 | Bob        |
|         4 | David      |
|         5 | John       |
|         6 | James      |
|         7 | Robert     |
|         8 | Carol      |
|         9 | Mike       |
|        10 | Maxwell    |
+-----------+------------+
10 rows in set (0.00 sec)
raja
Published on 19-Mar-2019 15:02:46
Advertisements