Name

MIN()

Synopsis

MIN(expression)

This function returns the lowest number in the values for a given column. It’s normally used in conjunction with a GROUP BY clause specifying a unique column, so that values are compared for each unique item separately. Here is an example:

SELECT CONCAT(name_first, SPACE(1), name_last) AS rep_name, 
MIN(sale_amount) AS smallest_sale,
MAX(sale_amount) AS biggest_sale
FROM sales
JOIN sales_reps USING(sales_rep_id)
GROUP BY sales_rep_id;

In this example, we retrieve the smallest sale and largest sale made by each sales representative. We use JOIN to join the two tables to get the sales rep’s name. Because MAX() is very similar, see the examples in its description earlier in this chapter for additional ways to use MIN().

Get MySQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.