SQL
SQL MIN(): Syntax, Usage, and Examples
The MIN() function returns the smallest value in a set of rows. It’s one of the fastest ways to answer questions like “what’s the lowest price?” or “what’s the earliest date?” in your database.
How to Use the MIN() function
MIN() takes a column (or expression) and returns a single smallest value.
Learn SQL on Mimo
SELECTMIN(column_name)
FROM table_name;
You can use it on numbers, dates, and even text, as long as the database knows how to compare the values.
Finding the minimum value in a column
SELECTMIN(price)AS lowest_price
FROM products;
This returns the smallest price in the products table.
Using MIN() with a filter
Most real queries need a filter. That’s where WHERE comes in.
SELECTMIN(amount)AS smallest_order
FROM orders
WHERE status='completed';
This returns the smallest completed order amount.
Using MIN() with groups
If you want a minimum per category, per customer, or per day, combine MIN() with GROUP BY.
SELECT category,MIN(price)AS lowest_price
FROM products
GROUPBY category;
Now you get one minimum per category.
Using MIN() on an expression
You can also apply MIN() to a calculation.
SELECTMIN(price* quantity)AS smallest_line_total
FROM order_items;
That returns the smallest total per item line.
When to Use the MIN() function
MIN() is useful whenever you need the smallest value instead of a full list of results.
1) Finding “first” events or earliest timestamps
This is common in analytics, onboarding, and logging tables.
Examples:
- Earliest signup date for a user
- First time someone opened an app
- First support ticket created in a week
2) Finding the cheapest, smallest, or lowest record
This is the classic “minimum value” use case.
Examples:
- Lowest product price
- Lowest shipping cost
- Smallest invoice total
3) Comparing against a threshold
Sometimes you need the minimum so you can compare it to a rule.
Examples:
- Lowest score in an exam system
- Minimum stock level in inventory tracking
- Minimum temperature recorded by a sensor
4) Data checks and cleanup
When data looks weird, MIN() helps you spot bad values quickly.
Examples:
- Negative prices
- Old dates like 1900-01-01
- Unexpected empty strings in a field
Examples of the MIN() function
Let’s look at a few practical examples you might actually use.
Example 1: Lowest product price
SELECTMIN(price)AS lowest_price
FROM products;
This gives you a single number like 4.99.
Example 2: Earliest signup date
Imagine a users table with created_at.
SELECTMIN(created_at)AS first_signup
FROM users;
If you’re curious when the platform started getting users, this answers it instantly.
Example 3: Cheapest product per category
SELECT category,MIN(price)AS lowest_price
FROM products
GROUPBY category;
You’ll get something like:
- Accessories → 3.50
- Shoes → 29.99
- Jackets → 79.00
Example 4: Lowest rating per course
Let’s say you store reviews like this:
course_idrating(1 to 5)
SELECT course_id,MIN(rating)AS lowest_rating
FROM course_reviews
GROUPBY course_id;
This helps you spot courses that received at least one rough review.
Example 5: Finding the minimum from recent rows only
Sometimes “minimum overall” isn’t helpful, you want the minimum in a time window.
SELECTMIN(amount)AS smallest_order_last_30_days
FROM orders
WHERE created_at>=CURRENT_DATE-INTERVAL'30 days';
This returns the smallest order amount in the last 30 days.
Example 6: Minimum value with a join
Example: minimum order amount per customer name.
SELECT c.name,MIN(o.amount)AS smallest_order
FROM customers c
JOIN orders oON o.customer_id= c.id
GROUPBY c.name
ORDERBY smallest_orderASC;
This is useful if you want to compare customers and see who tends to place tiny orders.
Learn More About the MIN() function
MIN() works with numbers, dates, and text
MIN() doesn’t only work on numbers.
- For dates,
MIN()returns the earliest date. - For text,
MIN()returns the “smallest” value based on alphabetical order.
Example with text:
SELECTMIN(username)AS first_username_alphabetically
FROM users;
If your database sorts strings by A to Z, the result might be "aaliyah".
Text comparisons can depend on collation rules (case sensitivity, locale), so keep that in mind if results surprise you.
MIN() ignores NULL values
If a column contains NULL, MIN() skips those rows instead of treating them as “the smallest.”
Example:
- 10
- 15
- NULL
The minimum is 10.
SELECTMIN(score)AS min_score
FROM exams;
If all values are NULL, the result is NULL.
Using COALESCE() with MIN()
If you want to treat NULL values as something else, wrap the column with COALESCE().
Example: treat missing prices as 0.
SELECTMIN(COALESCE(price,0))AS min_price
FROM products;
This is useful when NULL means “not entered yet” but you still want a numeric answer.
MIN() vs ORDER BY + LIMIT 1
You can also find the smallest value with sorting:
SELECT price
FROM products
ORDERBY priceASC
LIMIT1;
This returns the smallest price too, but it’s not the same as MIN().
Here’s the difference:
MIN()returns just the minimum value.ORDER BY ... LIMIT 1returns the row’s column value, and you can include other columns too.
If you want the full row (like product name + price), sorting is often the easiest option.
SELECT name, price
FROM products
ORDERBY priceASC
LIMIT1;
Getting the row that contains the minimum value
A common beginner mistake is trying to select multiple columns with MIN() without grouping.
This doesn’t work the way people expect:
-- This is NOT valid in many SQL databases
SELECT name,MIN(price)
FROM products;
If you want the product with the lowest price, use a subquery:
SELECT name, price
FROM products
WHERE price= (SELECTMIN(price)FROM products);
If multiple products share the same lowest price, this returns all of them, which is usually a nice bonus.
MIN() with GROUP BY and HAVING
HAVING filters grouped results based on aggregated values.
Example: show only categories where the cheapest item is under 10.
SELECT category,MIN(price)AS lowest_price
FROM products
GROUPBY category
HAVINGMIN(price)<10;
WHERE can’t do this, because WHERE runs before the grouping happens.
MIN() with CASE for conditional minimums
You can combine MIN() with CASE to find the minimum value inside a subset of rows.
Example: cheapest discounted price, ignoring full-price products.
SELECTMIN(
CASEWHEN is_discounted=TRUETHEN priceELSENULLEND
)AS cheapest_discounted_price
FROM products;
Using NULL in the ELSE keeps non-discounted items out of the calculation.
MIN() in window functions
Some databases support window functions, which let you compute a minimum without collapsing rows into one result per group.
Example: show each order and also show the smallest order amount for that customer.
SELECT
customer_id,
amount,
MIN(amount)OVER (PARTITIONBY customer_id)AS smallest_order_for_customer
FROM orders;
This is great for analysis, ranking, and debugging.
Watch out for accidental duplicates in joins
MIN() is usually safe, but joins can still mess up your meaning.
Example: if an orders table joins to order_items, every order repeats once per item. Your minimum might still be correct, but your query could be slower and harder to reason about.
A good habit:
- Check the join result row count
- Confirm you’re calculating the minimum over the values you actually care about
Summary
MIN() returns the smallest value in a column or expression, and it works on numbers, dates, and text. Use it for finding the earliest timestamps, cheapest prices, smallest totals, and quick data sanity checks.
Combine it with WHERE for filtering, GROUP BY for per-group minimums, and subqueries when you need the full row that contains the minimum value.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot