Select first row from MySQL table
Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table.
In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a particular column. For example, select the first row from the manager table where first_name is Mehvish.
In the second scenario, we will select the first row in the table per group. Here we will also see how to retrieve group-related data. In the third scenario, we will see how to get the first row from the entire MySQL table.
Select first row from MySQL table
Before diving into the code examples, let's create a manager_id table with manager_id, first_name, and last_name as fields, with manager_id being the primary key.
Given below are the queries to create and populate this table. You can also use these.
Sample code:
CREATE TABLE manager (
manager_id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(150) NOT NULL,
last_name VARCHAR(150) NOT NULL,
PRIMARY KEY(manager_id)
);
INSERT INTO manager (first_name, last_name)
VALUES
('Mehvish', 'Ashiq'),
('Saira', 'Mushtaq'),
('Thomas', 'Christopher'),
('Thomas', 'Gabrial'),
('Tahir', 'Raza'),
('Saira', 'Johny'),
('Saira', 'Daniel');
SELECT * FROM manager;
Output:
+------------+------------+-------------+
| manager_id | first_name | last_name |
+------------+------------+-------------+
| 1 | Mehvish | Ashiq |
| 2 | Saira | Mushtaq |
| 3 | Thomas | Christopher |
| 4 | Thomas | Gabrial |
| 5 | Tahir | Raza |
| 6 | Saira | Johny |
| 7 | Saira | Daniel |
+------------+------------+-------------+
7 rows in set (0.00 sec)
Use LIMIT clause to retrieve first row from MySQL table where one column has multiple instances
We have one instance of Mehvish, one instance of Tahir, two instances of Thomas, and three instances of Saira in the first_name column of the manager table. You can view the current table data using the SELECT statement.
Execute the following statement to get the first row whose first_name is Saira from the manager table.
SELECT * FROM manager WHERE first_name = 'Saira' LIMIT 1;
Output:
+------------+------------+-----------+
| manager_id | first_name | last_name |
+------------+------------+-----------+
| 2 | Saira | Mushtaq |
+------------+------------+-----------+
1 row in set (0.00 sec)
Suppose we want to fetch the third record where first_name is Saira. We use the LIMIT clause (explained later in this tutorial) with two parameters.
SELECT * FROM manager WHERE first_name = 'Saira' LIMIT 2,1;
Output
+------------+------------+-----------+
| manager_id | first_name | last_name |
+------------+------------+-----------+
| 7 | Saira | Daniel |
+------------+------------+-----------+
1 row in set (0.00 sec)
Suppose we want to get the first two records where first_name is Saira. We can do it like this.
SELECT * FROM manager WHERE first_name = 'Saira' LIMIT 2;
Output:
+------------+------------+-----------+
| manager_id | first_name | last_name |
+------------+------------+-----------+
| 2 | Saira | Mushtaq |
| 6 | Saira | Johny |
+------------+------------+-----------+
2 rows in set (0.00 sec)
We can also get the last two records where the first_name is Saira. We use the ORDER BY clause along with the LIMIT clause.
SELECT * FROM manager WHERE first_name = 'Saira' ORDER BY manager_id DESC LIMIT 2;
Output:
+------------+------------+-----------+
| manager_id | first_name | last_name |
+------------+------------+-----------+
| 7 | Saira | Daniel |
| 6 | Saira | Johny |
+------------+------------+-----------+
2 rows in set (0.00 sec)
Here, it is crucial to understand the role of the LIMIT
and ORDER BY
clauses. LIMIT
The clause SELECT
is used with the statement to fetch a certain number of records from a table.
LIMIT
The clause takes one or two arguments which must be positive integers or zero. Following is the syntax of the LIMIT clause with one and two arguments.
grammar:
# Syntax with one argument
SELECT
your_select_list
FROM
you_table_name
WHERE
your_condition
LIMIT row_count;
# Syntax with two arguments
SELECT
your_select_list
FROM
your_table_name
WHERE
your_condition
LIMIT [offset,] row_count;
row_count shows the maximum number of records to be returned, while offset is used when we want to retrieve a specific range of records, for example, from offset 2 to row_count 4. Remember that the offset of the first row is not 1, but 0.
See the picture below.
Remember, LIMIT row_count and LIMIT 0, row_count are equivalent. ORDER BY
The clause sorts the table data in descending or ascending order, depending on the project requirements.
Get the first row in each group from a MySQL table using IN(), MIN(), and GROUP BY
The second case uses the IN() and MIN() functions along with the GROUP BY clause to select from the table and get the first row in each group.
Sample code:
SELECT * FROM manager
WHERE
manager_id IN (
SELECT min(manager_id)
FROM manager
GROUP BY first_name
);
Output:
+------------+------------+-------------+
| manager_id | first_name | last_name |
+------------+------------+-------------+
| 1 | Mehvish | Ashiq |
| 2 | Saira | Mushtaq |
| 3 | Thomas | Christopher |
| 5 | Tahir | Raza |
+------------+------------+-------------+
4 rows in set (0.08 sec)
This example uses different functions and clauses to achieve the desired results.
- IN() − This function allows us to specify multiple values in the WHERE clause. It returns 1 if the expression is equal to any of the IN() list values.
-
GROUP BY − It groups the records containing same values and is mainly used for aggregate functions like
MIN()
,MAX()
,COUNT()
etc. - MIN() − It returns the minimum value of an attribute (column) in a query.
- Subquery - It is nested inside another query like SELECT, UPDATE, DELETE etc. We can also nest a subquery inside another subquery (we need to understand the order of execution of various clauses).
Use LIMIT and ORDER BY to select the first row from the entire MySQL table
Use the LIMIT clause to select the first row from the entire table, regardless of order and condition.
SELECT * from manager LIMIT 1;
Output:
+------------+------------+-----------+
| manager_id | first_name | last_name |
+------------+------------+-----------+
| 1 | Mehvish | Ashiq |
+------------+------------+-----------+
1 row in set (0.00 sec)
Use ORDER BY with LIMIT to get the first row from the sorted data (in descending order).
SELECT * from manager ORDER BY first_name DESC LIMIT 1;
Output:
+------------+------------+-------------+
| manager_id | first_name | last_name |
+------------+------------+-------------+
| 3 | Thomas | Christopher |
+------------+------------+-------------+
1 row in set (0.00 sec)
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Display tables and database structure in MySQL
Publish Date:2025/04/23 Views:97 Category:MySQL
-
Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi
Converting from datetime type to date-only in MySQL
Publish Date:2025/04/23 Views:199 Category:MySQL
-
Today, we will learn the DATE(), CAST(), CONVERT() and DATE_FORMAT() methods to convert DATETIME type to DATE type in MySQL. The above mentioned methods can be used in MySQL 4.0 and above. Converting from DATETIME to DATE in MySQL To unders
Changing max_allowed_packet Size in MySQL Server
Publish Date:2025/04/22 Views:193 Category:MySQL
-
This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi
Zerofill usage, advantages and alternatives in MySQL
Publish Date:2025/04/22 Views:196 Category:MySQL
-
In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input
Compare only MySQL timestamp dates to date parameters
Publish Date:2025/04/22 Views:64 Category:MySQL
-
In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi
Calculating Percentages in MySQL
Publish Date:2025/04/22 Views:67 Category:MySQL
-
We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re
Selecting multiple values using WHERE in MySQL
Publish Date:2025/04/22 Views:186 Category:MySQL
-
This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi
Changing the connection timeout in MySQL
Publish Date:2025/04/22 Views:59 Category:MySQL
-
We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti
MySQL fix Data Is Truncated for a Column error
Publish Date:2025/04/22 Views:102 Category:MySQL
-
This article describes possible causes and solutions for the MySQL error Data is truncated for a column . Fix data truncated due to column error in MySQL Here, we will discuss the possible causes and solutions to eliminate MySQL data trunca