
- 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
How to order records and fetch some using MySQL LIMIT?
Let us first create a table −
mysql> create table DemoTable679(FirstName varchar(100)); Query OK, 0 rows affected (0.44 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable679 values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable679 values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable679 values('David'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable679 values('Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable679 values('Mike'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable679 values('Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable679 values('Robert'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable679;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | John | | Chris | | David | | Bob | | Mike | | Sam | | Robert | +-----------+ 7 rows in set (0.00 sec)
Following is the query to select the last n rows in MySQL. Here, we are first ordering it in ASC and displaying the first 4 records −
mysql> select * from DemoTable679 order by FirstName Limit 0,4;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Bob | | Chris | | David | | John | +-----------+ 4 rows in set (0.00 sec)
- Related Articles
- Fetch ordered records after a specific limit in MySQL
- Order date records and fetch the 2nd ordered record in MySQL
- Fetch records from comma separated values using MySQL IN()?
- MySQL query to find a match and fetch records
- Implement MySQL REGEXP to fetch records with . and numbers
- Fetch records on the basis of LastName using MySQL IN()
- Get records in a certain order using MySQL?
- How to limit records to only the last five results in MySQL
- How to fetch the newly added records from a MySQL table?
- MySQL to fetch records based on a specific month and year?
- MySQL query to fetch records before currentdate + 2 weeks?
- Using aggregation pipeline to fetch records in MongoDB
- Order MySQL records randomly and display name in Ascending order
- How to order records by a column in MySQL and place empty records at the end?
- MySQL query for text search with LIKE and OR to fetch records

Advertisements