Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Display all products having the highest amount in a MySQL table with product details?
Use MAX() along with subquery for this Here, MAX() is used to get the maximum amount. Let us first create a table −
mysql> create table DemoTable ( ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ProductName varchar(100), ProductAmount int ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(ProductName,ProductAmount) values('Product-1',60);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(ProductName,ProductAmount) values('Product-2',40);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable(ProductName,ProductAmount) values('Product-3',75);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(ProductName,ProductAmount) values('Product-4',50);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(ProductName,ProductAmount) values('Product-5',75);
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 | ProductName | ProductAmount | +-----------+-------------+---------------+ | 1 | Product-1 | 60 | | 2 | Product-2 | 40 | | 3 | Product-3 | 75 | | 4 | Product-4 | 50 | | 5 | Product-5 | 75 | +-----------+-------------+---------------+ 5 rows in set (0.00 sec)
Following is the query to display all the products having the maximum amount −
mysql> select *from DemoTable where ProductAmount=(select max(ProductAmount) from DemoTable);
This will produce the following output −
+-----------+-------------+---------------+ | ProductId | ProductName | ProductAmount | +-----------+-------------+---------------+ | 3 | Product-3 | 75 | | 5 | Product-5 | 75 | +-----------+-------------+---------------+ 2 rows in set (0.00 sec)
Advertisements
