MIN
MIN returns the minimum non-null value in a set.
Use it for earliest dates, smallest amounts, or alphabetically first text.
Core Concepts
flowchart LR
A[Input values] --> B[Compare values]
B --> C[Return smallest non-NULL value]
| Use case | Example |
|---|---|
| Smallest amount | MIN(amount) |
| Earliest date text | MIN(order_date) |
| First name alphabetically | MIN(name) |
Code Examples
-- Setup simple table.
CREATE TABLE orders_min (
order_id INTEGER PRIMARY KEY,
order_date TEXT NOT NULL,
total REAL NOT NULL
);
INSERT INTO orders_min (order_date, total)
VALUES
('2026-03-01', 99.0),
('2026-02-15', 49.0),
('2026-03-10', 149.0);
| Expected output |
|---|
| Table created and three rows inserted. |
-- Earliest date and smallest total.
SELECT
MIN(order_date) AS earliest_date,
MIN(total) AS smallest_total
FROM orders_min;
| earliest_date | smallest_total |
|---|---|
| 2026-02-15 | 49.0 |
SQLite-Specific Nuances
SQLite Nuance
For date-like text, MIN works correctly when dates are stored in sortable ISO-8601 format (YYYY-MM-DD).
Common Pitfalls / Best Practices
Pitfall
Storing dates in inconsistent text formats and expecting MIN/MAX to behave chronologically.
Best Practice
Store dates as ISO-8601 text (YYYY-MM-DD or YYYY-MM-DD HH:MM:SS) for reliable ordering and aggregation.
Quick Challenge
Return the earliest order_date from orders_min.
View Solution
SELECT MIN(order_date) AS earliest_date
FROM orders_min;
| earliest_date |
|---|
| 2026-02-15 |