
- 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 return the nth record from MySQL query?
To get the nth record from MySQL query, you can use LIMIT. The syntax is as follows −
select *from yourTableName order by yourColumnName limit n,1;
To understand the above syntax, let us create a table. The following is the query to create a table −
mysql> create table NthRecordDemo −> ( −> Id int, −> Name varchar(200) −> ); Query OK, 0 rows affected (0.92 sec)
Insert some records in the table using the following query −
mysql> insert into NthRecordDemo values(100,'John'); Query OK, 1 row affected (0.09 sec) mysql> insert into NthRecordDemo values(101,'Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into NthRecordDemo values(102,'Carol'); Query OK, 1 row affected (0.22 sec) mysql> insert into NthRecordDemo values(103,'Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into NthRecordDemo values(104,'Johnson'); Query OK, 1 row affected (0.16 sec) mysql> insert into NthRecordDemo values(105,'Sam'); Query OK, 1 row affected (0.16 sec) mysql> insert into NthRecordDemo values(106,'David'); Query OK, 1 row affected (0.13 sec)
Display all records from the table with the help of select statement. The query is as follows −
mysql> select *from NthRecordDemo;
The following is the output −
+------+---------+ | Id | Name | +------+---------+ | 100 | John | | 101 | Bob | | 102 | Carol | | 103 | Smith | | 104 | Johnson | | 105 | Sam | | 106 | David | +------+---------+ 7 rows in set (0.00 sec)
Use the following query to get nth record from the table −
mysql> select *from NthRecordDemo order by Id limit 6,1;
The following is the output −
+------+-------+ | Id | Name | +------+-------+ | 106 | David | +------+-------+ 1 row in set (0.00 sec)
- Related Articles
- How to get a specific column record from SELECT query in MySQL?
- Get the new record key ID from MySQL insert query?
- MySQL query to find expiry date (record) from the next 2 days?
- MySQL query to search record with apostrophe?
- MySQL query to fetch record by year
- MySQL query to delete a record with the lowest ID?
- MySQL query to place a specific record on the top
- Query to find Nth maximum value in MySQL
- MySQL query to return 5 random records from last 20 records?
- How to find the previous and next record using a single query in MySQL?
- MySQL query to decrease the value of a specific record to zero?
- How can I return a record from a table nearest to a user-defined variables value in MySQL?
- MySQL query to select a record with two exact values?
- MySQL query to return the count of only NO values from corresponding column value
- How to select a random record from a MySQL database?

Advertisements