
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Get at least x number of rows in MySQL?
To get at least x number of rows, you need to use the LIMIT clause. Following is the syntax −
select *from yourTableName order by yourColumnName DESC limit yourXNumberOfRows;
Let us first create a table −
mysql> create table DemoTable ( EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,EmployeeName varchar(100) ); Query OK, 0 rows affected (0.76 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(EmployeeName) values('Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(EmployeeName) values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(EmployeeName) values('Bob'); Query OK, 1 row affected (0.51 sec) mysql> insert into DemoTable(EmployeeName) values('Robert'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable(EmployeeName) values('Mike'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(EmployeeName) values('Sam'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 1 | Chris | | 2 | David | | 3 | Bob | | 4 | Robert | | 5 | Mike | | 6 | Sam | +------------+--------------+ 6 rows in set (0.00 sec)
Following is the query to get at least x number of rows in MySQL. Here, we are displaying only 2 rows −
mysql> select *from DemoTable order by EmployeeName DESC limit 2;
This will produce the following output −
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 6 | Sam | | 4 | Robert | +------------+--------------+ 2 rows in set (0.02 sec)
- Related Articles
- Get total number of rows while using LIMIT in MySQL?
- Can we get total number of rows in a MySQL database?
- Get the number of rows in a particular table with MySQL
- Easiest way to get number of rows in a MySQL table?
- Find the smallest number X such that X! contains at least Y trailing zeros in C++
- Largest Number At Least Twice of Others in Python
- Get the size of selected rows in MySQL
- How to get number of rows in a table without using count(*) MySQL query?
- How to remove rows that contain at least one 0 in R?
- How can we get the total number of rows affected by MySQL query?
- Get at least one match in list querying with MongoDB?
- Get rows with GROUP BY in MySQL?
- Maximum average of a subarray of size of at least X and at most Y in C++
- Count number of paths whose weight is exactly X and has at-least one edge of weight M in C++
- Count number of rows in each table in MySQL?

Advertisements