MySQL Get Maximum Value

Write a mysql statement to get item id, item, price of the most expensive item.

Suppose the item table is -
+-----------+--------------+----------------+
| ITEM_ID   | ITEM         | PRICE          |
+-----------+--------------+----------------+
| 1001      | Book         | 1200           |
| 1002      | Pen          | 930            |
| 1003      | Bag          | 1430           |
| 1004      | Copy         | 1030           |
+-----------+--------------+----------------+

Solution

MySQL MAX() function returns maximum value in a set of values. It accepts one argument i.e. the expression and returns the highest value of the expression. This expression is the required parameter.

The following statement returns maximum price in set of price list of items.

SELECT  ITEM_ID, ITEM, PRICE
FROM ITEM
WHERE price=(SELECT MAX(price) FROM ITEM); 

Output of the above code -

+---------+------+-------+
| ITEM_ID | ITEM | PRICE |
+---------+------+-------+
|    1003 | Bag  |  1430 |
+---------+------+-------+




Related MySQL Exercises

MySQL concatenate
MySQL Get Current Date, User, Version
MySQL Administrator
MySQL where clause
MySQL order by
MySQL get difference between two dates
MySQL Pattern Matching
MySQL Join
MySQL Regular Expression
MySQL delete duplicate row
MySQL update multiple rows
Get nth highest salary using MySQL
Display the nth row from MySQL
Commit and rollback in mysql
MySQL SELECT top 5
MySQL display the alternate rows




Read more articles


General Knowledge



Learn Popular Language