MySQL 5.1 Reference Manual  /  ...  /  The Maximum Value for a Column

3.6.1 The Maximum Value for a Column

What is the highest item number?

SELECT MAX(article) AS article FROM shop;

| article |
|       4 |

Download this Manual
User Comments
  Posted by Najeem M Illyas on November 13, 2004
Similarly MIN can be used to find minium value

SELECT MIN(jnr_id) AS jnr_id FROM chain

| jnr_id |
| 1000 |
  Posted by Andy Pieters on February 6, 2005
You can use just about any function in a select query.

Besides the MAX, and MIN functions, there is also the AVG function to quickly calculate the Average value of a field.

* The AS parameter sets the name of the field that is outputted


SELECT avg(article) AS article_avg FROM shop;

| article_avg |
| 2.4286 |
1 row in set (0.00 sec)

  Posted by Alex Grim on December 13, 2006
Also, you can use sum:

mysql> select sum(article) as Total from shop;
| Total |
| 17 |

  Posted by Sony George on February 18, 2010
How to display 4th highest (salary) record from customer table:

mysql> SELECT sal FROM `emp` order by sal desc limit 3,1
  Posted by Kobus Myburgh on November 5, 2013
I can not find anything confirming this, but my tests indicate that if you use MAX() on a field with an alpha-numeric value (such as ETA-000001), it does calculate the MAX() correctly, providing the number is padded by 0's.

To get a better (and probably more syntactically correct) result, you could use the following:

MAX(SUBSTRING(reference_number FROM 5 FOR 6))

for the example number given above.
Sign Up Login You must be logged in to post a comment.