

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to fetch multiple least values?
For this, you can use a sub query along with MIN(). Let us first create a table−
mysql> create table DemoTable -> ( -> Name varchar(20), -> Score int -> ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John',56); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('John',45); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('John',58); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Chris',43); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Chris',38); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris',87); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement−
mysql> select *from DemoTable;
This will produce the following output−
+-------+-------+ | Name | Score | +-------+-------+ | John | 56 | | John | 45 | | John | 58 | | Chris | 43 | | Chris | 38 | | Chris | 87 | +-------+-------+ 6 rows in set (0.00 sec)
Here is the query to fetch multiple least values−
mysql> select *from DemoTable tbl1 -> where Score IN( select min(Score) from DemoTable tbl2 where tbl1.Name=tbl2.Name);
This will produce the following output−
+-------+-------+ | Name | Score | +-------+-------+ | John | 45 | | Chris | 38 | +-------+-------+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- Use LIKE % to fetch multiple values in a single MySQL query
- MongoDB query to fetch array values
- Can we fetch multiple values with MySQL WHERE Clause?
- How to fetch fields with multiple values set using MySQL LIKE?
- MySQL query to fetch the maximum corresponding value from duplicate column values
- MySQL query to fetch record by year
- MySQL query to fetch specific records matched from an array (comma separated values)
- Fetch multiple documents in MongoDB query with OR condition?
- MySQL query to fetch the maximum cumulative value
- How to use MySQL LIKE clause to fetch multiple values beginning with “Joh”
- Fastest way to insert with multiple values in a single MySQL query?
- A single MySQL query to search multiple words from different column values
- MySQL Select Multiple VALUES?
- MongoDB query to pull multiple values from array
- MySQL query to fetch date with year and month?
Advertisements