
- 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
Pull row with lowest number in a MySQL column?
Use aggregate function MIN() along with GROUP BY for this. Here, we will display the minimum ID for NumberOfProduct . Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, NumberOfProduct int ); Query OK, 0 rows affected (0.19 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(NumberOfProduct) values(40); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(NumberOfProduct) values(40); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(NumberOfProduct) values(60); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(NumberOfProduct) values(60); Query OK, 1 row affected (0.06 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-----------------+ | Id | NumberOfProduct | +----+-----------------+ | 1 | 40 | | 2 | 40 | | 3 | 60 | | 4 | 60 | +----+-----------------+ 4 rows in set (0.00 sec)
Following is the query to pull row with lowest number in a column −
mysql> select NumberOfProduct,MIN(Id) from DemoTable group by NumberOfProduct;
This will produce the following output −
+-----------------+---------+ | NumberOfProduct | MIN(Id) | +-----------------+---------+ | 40 | 1 | | 60 | 3 | +-----------------+---------+ 2 rows in set (0.00 sec)
- Related Articles
- Get row data for the lowest and highest values in a MySQL column
- Multiplying column with NULL row in MySQL?
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Write a program in Python to find the lowest value in a given DataFrame and store the lowest value in a new row and column
- Find the next lowest number higher than a certain number in MySQL?
- How to count number of NULLs in a row with MySQL?
- MySQL query to select a row which contains same number in a column with set of numbers separated by comma?
- How can we combine ROW selection with COLUMN selection in MySQL?
- MySQL insert a value to specific row and column
- Count the same value of each row in a MySQL column?
- The Row Holding the Maximum of a Certain Column in MySQL
- Return value from a row if it is NOT NULL, else return the other row value in another column with MySQL
- MySQL query to delete a record with the lowest ID?
- Find the column number with largest value for each row in an R matrix.
- How to select the sum of the column values with higher value in reach row with MySQL?

Advertisements