
- 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 select first and last row record using LIMIT in MySQL?
Let us first create a table. The query to create a table is as follows
mysql> create table FirstAndLastDataDemo -> ( -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> EmployeeName varchar(20), -> EmployeeAge int -> ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command. The query is as follows
mysql> insert into FirstAndLastDataDemo(EmployeeName,EmployeeAge) values('John',23); Query OK, 1 row affected (0.15 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName,EmployeeAge) values('Bob',13); Query OK, 1 row affected (0.11 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName,EmployeeAge) values('Larry',24); Query OK, 1 row affected (0.14 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName,EmployeeAge) values('Sam',14); Query OK, 1 row affected (0.18 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName,EmployeeAge) values('Mike',31); Query OK, 1 row affected (0.10 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName,EmployeeAge) values('James',18); Query OK, 1 row affected (0.16 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName,EmployeeAge) values('Maxwell',28); Query OK, 1 row affected (0.17 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName,EmployeeAge) values('David',27); Query OK, 1 row affected (0.12 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName,EmployeeAge) values('Chris',22); Query OK, 1 row affected (0.19 sec)
Display all records from the table using a select statement.
mysql> select *from FirstAndLastDataDemo;
The following is the output
+------------+--------------+-------------+ | EmployeeId | EmployeeName | EmployeeAge | +------------+--------------+-------------+ | 1 | John | 23 | | 2 | Bob | 13 | | 3 | Larry | 24 | | 4 | Sam | 14 | | 5 | Mike | 31 | | 6 | James | 18 | | 7 | Maxwell | 28 | | 8 | David | 27 | | 9 | Chris | 22 | +------------+--------------+-------------+ 9 rows in set (0.00 sec)
To get first row and last row record using LIMIT, the following is the query
mysql> (SELECT *from FirstAndLastDataDemo LIMIT 1) -> UNION -> (SELECT *from FirstAndLastDataDemo LIMIT 8,1);
The following is the output displaying the first and last row record
+------------+--------------+-------------+ | EmployeeId | EmployeeName | EmployeeAge | +------------+--------------+-------------+ | 1 | John | 23 | | 9 | Chris | 22 | +------------+--------------+-------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to select first and last data row from a MySQL result?
- SELECT last entry without using LIMIT in MySQL?
- How to select last row in MySQL?
- Select last record and update it in MySQL?
- MySQL LIMIT to select a single row
- How to select record from last 6 months in a news table using MySQL?
- How to get the first and last record of the table in MySQL?
- Can we select second largest record from a table without using LIMIT clause in MySQL?
- Insert record using MySQL SELECT?
- MySQL limit in a range fail to display first 3 row?
- How to select the top two values using LIMIT in MySQL?
- How to select the first and last row based on group column in an R data frame?
- MySQL query to select rows except first row in descending order?
- How to select last two rows in MySQL?
- How to select next row pagination in MySQL?

Advertisements