SQL Keywords

SQL MIN

What does the SQL MIN function do?

MIN returns the smallest non-NULL value in a column or expression.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL MIN: PostgreSQL, MySQL, SQL Server, Oracle, SQLite, MariaDB, Snowflake, BigQuery

SQL MIN Full Explanation

MIN is an aggregate (and window) function that scans the input set and returns the minimum non-NULL value. If all values are NULL, the result is NULL. MIN works on numeric, date/time, and text data types, following the database’s default collation rules for text. In GROUP BY queries, it produces one result per group. As a window function it can compute running or partitioned minimums without collapsing rows. Because MIN ignores NULL, use COALESCE or FILTER clauses when you need to treat NULL as a real value. DISTINCT inside MIN is legal in most systems but has no practical effect because the minimum of a set is the same whether duplicates exist or not.

SQL MIN Syntax

-- Aggregate
SELECT MIN([DISTINCT] expression)
FROM table_name
[WHERE condition];

-- Window
SELECT MIN(expression) OVER (
        [PARTITION BY column_list]
        [ORDER BY sort_list]
        [ROWS|RANGE frame]
) AS min_val
FROM table_name;

SQL MIN Parameters

  • expression (any) - The column or expression whose minimum is sought
  • DISTINCT (optional) - Ignored for MIN in most dialects but syntactically allowed
  • PARTITION BY (optional) - Window partitions
  • ORDER BY (optional) - Defines row order inside the window
  • ROWS/RANGE (optional) - Frame specification for windowed minimum

Example Queries Using SQL MIN

-- Smallest order total across all orders
SELECT MIN(total_amount) AS lowest_order
FROM orders;

-- Minimum signup date per plan
SELECT plan_id, MIN(signup_date) AS first_signup
FROM users
GROUP BY plan_id;

-- Running minimum balance by account
SELECT account_id,
       txn_date,
       balance,
       MIN(balance) OVER (PARTITION BY account_id ORDER BY txn_date) AS running_min_balance
FROM account_transactions;

Expected Output Using SQL MIN

  • Each query returns a single column containing the smallest non NULL value overall, per group, or per window partition, respectively

Use Cases with SQL MIN

  • Find the earliest date a customer performed an action
  • Identify the lowest price of a product in a price history table
  • Compute running minimums for monitoring minimum thresholds
  • Combine with GROUP BY to get per category minimum metrics

Common Mistakes with SQL MIN

  • Expecting MIN to consider NULL as 0 or a low sentinel
  • Forgetting GROUP BY columns when selecting other fields alongside MIN
  • Thinking MIN(DISTINCT col) behaves differently from MIN(col)
  • Using MIN on strings without realizing collation affects ordering

Related Topics

MAX, AVG, SUM, COUNT, GROUP BY, HAVING, WINDOW FUNCTIONS

First Introduced In

SQL-92 standard

Frequently Asked Questions

Does MIN include NULL values?

No. MIN ignores NULL values entirely. If every row is NULL, the function returns NULL.

How is MIN different from MAX?

MIN returns the smallest non NULL value, whereas MAX returns the largest. Both share identical syntax.

Can MIN be used as a window function?

Yes. Add the OVER clause to calculate running or partitioned minimums without collapsing rows.

Is MIN(DISTINCT column) necessary?

Not usually. Since MIN only needs the single smallest value, duplicate rows do not affect the result, making DISTINCT redundant.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!