← Back to Chapters

MySQL MIN() Function

? MySQL MIN()

? Quick Overview

The MIN() aggregate function returns the smallest value from a set of values. It's commonly used with numeric, date/time, or string columns (string comparison follows collation rules).

? Key Concepts

  • Aggregate function: operates on multiple rows and returns a single value.
  • NULL handling: MIN() ignores NULLs; if all values are NULL, result is NULL.
  • Grouping: use GROUP BY to compute minima per group.
  • Data types: works with numeric, temporal, and string types.

⚙️ Syntax / Theory

? View Code Example
-- Create a sample products table
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
Category VARCHAR(50),
Price DECIMAL(8,2),
AddedOn DATE
);
-- Insert sample rows
INSERT INTO Products (ProductID, Category, Price, AddedOn) VALUES
(1, 'Books', 199.99, '2024-01-12'),
(2, 'Books', 149.50, '2024-03-05'),
(3, 'Gadgets', 499.00, '2024-02-20'),
(4, 'Gadgets', NULL, '2024-05-01'),
(5, 'Books', 99.99, '2024-06-10');

? Common Examples

? View Code Example
-- 1) Minimum price overall
SELECT MIN(Price) AS MinPrice FROM Products;
? View Code Example
-- 2) Minimum price per category (grouped)
SELECT Category, MIN(Price) AS MinPrice
FROM Products
GROUP BY Category;
? View Code Example
-- 3) Conditional minimum using COALESCE
SELECT MIN(COALESCE(Price, 999999)) AS MinPriceIgnoringNulls FROM Products;

? Live Output / Explanation

Expected results (based on example inserts)

Result: 99.99 — lowest non-NULL price.

? Use Cases

  • Finding earliest dates.
  • Finding cheapest products.
  • Analytics and reporting.

? Tips & Best Practices

  • MIN() ignores NULL values.
  • Indexes improve MIN() performance.
  • Collation affects string comparison.

? Try It Yourself / Practice Tasks

  1. Create a table and find the lowest value.
  2. Use GROUP BY with MIN().
  3. Test behavior with all NULL values.