
- 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 data in MySQL where a field has a minimum value?
To select data where a field has min value, you can use aggregate function min(). The syntax is as follows.
SELECT *FROM yourTableName WHERE yourColumnName=(SELECT MIN(yourColumnName) FROM yourTableName);
To understand the above syntax, let us create a table. The query to create a table is as follows.
mysql> create table MinValueDemo -> ( -> ProductId int, -> ProductName varchar(100), -> ProductPrice int -> ); Query OK, 0 rows affected (0.77 sec)
Insert some records in the table using insert command. The query is as follows.
mysql> insert into MinValueDemo values(1,'product-1',4500); Query OK, 1 row affected (0.14 sec) mysql> insert into MinValueDemo values(2,'product-2',4340); Query OK, 1 row affected (0.22 sec) mysql> insert into MinValueDemo values(3,'product-3',4110); Query OK, 1 row affected (0.18 sec) mysql> insert into MinValueDemo values(4,'product-4',4344); Query OK, 1 row affected (0.16 sec) mysql> insert into MinValueDemo values(5,'product-5',4103); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement. The query is as follows.
mysql> select *from MinValueDemo;
The following is the output.
+-----------+-------------+--------------+ | ProductId | ProductName | ProductPrice | +-----------+-------------+--------------+ | 1 | product-1 | 4500 | | 2 | product-2 | 4340 | | 3 | product-3 | 4110 | | 4 | product-4 | 4344 | | 5 | product-5 | 4103 | +-----------+-------------+--------------+ 5 rows in set (0.00 sec)
Here is the query to select data where the ‘ProductPrice’ has the minimum value using aggregate function MIN() from MySQL.
mysql> select *from MinValueDemo -> where ProductPrice=(select min(ProductPrice) from MinValueDemo);
The following is the output.
+-----------+-------------+--------------+ | ProductId | ProductName | ProductPrice | +-----------+-------------+--------------+ | 5 | product-5 | 4103 | +-----------+-------------+--------------+ 1 row in set (0.08 sec)
- Related Articles
- How to select data from a table where the table name has blank spaces in MYSQL?
- Appending data to a MySQL field that already has data in it?
- How to select a row where one of several columns equals a certain value in MySQL?
- How do you select from MySQL where last value in a string = x?
- Concat a field in MySQL SELECT?
- How to generate field in MySQL SELECT?
- SELECT where row value contains string in MySQL?
- How to swap a specific field value in MySQL?
- How to derive value of a field from another field in MySQL?
- How to select objects where an array contains only a specific field in MongoDB?
- MySQL select any one field out of two with respect to the value of a third field?
- Fetch rows where a field value is less than 5 chars in MySQL?
- MySQL Select where value exists more than once
- Set an alternative of WHERE clause for each SELECT field in MySQL
- Select minimum row value from a column with corresponding duplicate column values in MySQL

Advertisements