
- 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
How to select a field corresponding to the field in which MAX() exists?
For this, you can use sub query along with aggregate function MAX(). Let us first create a table −
mysql> create table DemoTable -> ( -> ProductId int, -> ProductAmount int -> ); Query OK, 0 rows affected (0.78 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(1001,7895); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(1003,8903); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(1010,7690); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(2010,8450); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+---------------+ | ProductId | ProductAmount | +-----------+---------------+ | 1001 | 7895 | | 1003 | 8903 | | 1010 | 7690 | | 2010 | 8450 | +-----------+---------------+ 4 rows in set (0.00 sec)
Following is the query to select a field corresponding to the field in which max() exists −
mysql> select *from DemoTable -> where ProductAmount=( select max(ProductAmount) from DemoTable);
This will produce the following output −
+-----------+---------------+ | ProductId | ProductAmount | +-----------+---------------+ | 1003 | 8903 | +-----------+---------------+ 1 row in set (0.05 sec)
- Related Questions & Answers
- How to determine whether a field exists in MongoDB?
- How to select a single field in MongoDB?
- How to generate field in MySQL SELECT?
- How to apply a condition only if field exists in MongoDB?
- Concat a field in MySQL SELECT?
- Check that Field Exists with MongoDB?
- MongoDB. max length of field name?
- MySQL select any one field out of two with respect to the value of a third field?
- How to convert char field to datetime field in MySQL?
- How can I use MongoDB to find all documents which have a field, regardless of the value of that field?
- How to select data in MySQL where a field has a minimum value?
- Check if value exists for a field in a MongoDB document?
- How can I check whether a field exists or not in MongoDB?
- How to derive value of a field from another field in MySQL?
- MongoDB aggregate to get the count of field values of corresponding duplicate names?
Advertisements