

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to select first 10 elements from a MySQL 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)
- Related Questions & Answers
- How to write a MySQL query to select first 10 records?
- How to select last 10 rows from MySQL?
- How to select a random record from a MySQL database?
- MYSQL - select database?
- How to skip first 10 results in MySQL?
- How to select first and last data row from a MySQL result?
- MySQL query to select top 10 records?
- After connecting to MySQL server how can we select a database from command prompt?
- Select first word in a MySQL query?
- Select two random rows in a MySQL database?
- MySQL SELECT to skip first N results?
- How to select all the characters after the first 20 characters from a column in MySQL?
- How do I select four random tables from a MySQL database having thousands of tables?
- Which PHP function is used to select a MySQL database?
- Delete the first 10 characters from JTextArea in Java
Advertisements