0
0
SQLquery~5 mins

MIN and MAX functions in SQL

Choose your learning style9 modes available
Introduction
MIN and MAX functions help find the smallest or largest value in a group of data. They make it easy to compare numbers or dates.
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 week.
Syntax
SQL
SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;
MIN returns the smallest value in the chosen column.
MAX returns the largest value in the chosen column.
Examples
Finds the lowest price from the products table.
SQL
SELECT MIN(price) FROM products;
Finds the highest score from the game_scores table.
SQL
SELECT MAX(score) FROM game_scores;
Finds the earliest birthdate among employees.
SQL
SELECT MIN(birthdate) FROM employees;
Finds the highest salary in the Sales department.
SQL
SELECT MAX(salary) FROM employees WHERE department = 'Sales';
Sample Program
This creates a products table, adds three items with prices, then finds the lowest and highest price.
SQL
CREATE TABLE products (id INT, name VARCHAR(20), price DECIMAL(5,2));
INSERT INTO products VALUES (1, 'Pen', 1.20), (2, 'Notebook', 2.50), (3, 'Eraser', 0.80);
SELECT MIN(price) AS LowestPrice, MAX(price) AS HighestPrice FROM products;
OutputSuccess
Important Notes
MIN and MAX ignore NULL values in the column.
You can use these functions with numbers, dates, and even text (alphabetically).
They are often used with GROUP BY to find min or max per group.
Summary
MIN finds the smallest value in a column.
MAX finds the largest value in a column.
Both help quickly compare and find extremes in data.