0
0
MySQLquery~5 mins

MIN and MAX functions in MySQL

Choose your learning style9 modes available
Introduction

MIN and MAX help find the smallest or largest value in a list of data. They make it easy to spot extremes.

Finding the lowest price of products in a store.
Getting the highest score in a game leaderboard.
Checking the earliest or latest date in a list of events.
Finding the minimum or maximum temperature recorded in a month.
Syntax
MySQL
SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;

Use MIN to get the smallest value in a column.

Use MAX to get the largest value in a column.

Examples
This finds the lowest price in the products table.
MySQL
SELECT MIN(price) FROM products;
This finds the highest score in the game_scores table.
MySQL
SELECT MAX(score) FROM game_scores;
This finds the earliest birthdate in the users table.
MySQL
SELECT MIN(birthdate) FROM users;
This finds the highest salary in the employees table.
MySQL
SELECT MAX(salary) FROM employees;
Sample Program

This example creates a sales table, adds some sales amounts, then finds the smallest and largest sale.

MySQL
CREATE TABLE sales (
  id INT,
  amount DECIMAL(10,2)
);

INSERT INTO sales VALUES (1, 100.50), (2, 250.00), (3, 75.25), (4, 300.00);

SELECT MIN(amount) AS LowestSale, MAX(amount) AS HighestSale FROM sales;
OutputSuccess
Important Notes

MIN and MAX ignore NULL values automatically.

You can use these functions with numbers, dates, and even text (alphabetically).

Summary

MIN finds the smallest value in a column.

MAX finds the largest value in a column.

They help quickly find extremes in your data.