MIN Function

SQL · Aggregate Functions

Theory

MIN is the mirror of MAX: it returns the smallest value in a column across a set of rows. For numbers that is the lowest value, for dates the earliest date, and for text the value that sorts first alphabetically. NULL values are skipped, and MIN returns NULL only when there are no non-NULL values to compare.

With GROUP BY, MIN gives one result per group, such as the lowest salary in each department or the first order date of each customer (handy for finding when a customer joined). You can use MIN and MAX together to get a range, for example MAX(salary) - MIN(salary) per department. Text comparison depends on the database collation, so upper and lower case letters may sort differently than you expect.

Why we use it

Lowest price, earliest date, smallest stock level: MIN finds the floor of a set of values without sorting everything. Combined with MAX it gives you ranges, and with GROUP BY it gives you 'first time' facts per customer or product.

When to use it

Use MIN for the smallest number or the earliest date, for example a customer's first purchase. If you need the row behind the minimum, use ROW_NUMBER() or a join. For the minimum across columns in the same row, use LEAST() where available, not MIN.

How it works

  1. Rows arrive after FROM and WHERE
  2. GROUP BY splits rows into groups
  3. Start with no current minimum
  4. Is the next value NULL? Yes: Skip it. No: Keep it if it is smaller than the current minimum.
  5. Repeat for every row in the group
  6. Return the minimum (NULL if no values)

Syntax

MIN(column)

Example

SELECT MIN(salary) FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic