MIN() OVER

SQL · Window Functions

Theory

MIN(column) OVER (...) returns the smallest value in the window and shows it on every row. With PARTITION BY dept, each employee row carries the lowest salary of their department, so you can compute salary - dept_min or flag the lowest earner with CASE WHEN salary = dept_min THEN 'Yes' END, all in one query and without collapsing rows.

With ORDER BY inside OVER, the default frame runs from the start of the partition to the current row, so MIN becomes a running minimum: the lowest value seen so far. That is useful for tracking things like 'lowest price so far' or 'best (fastest) time so far'. NULLs are ignored. Unlike FIRST_VALUE, MIN only returns the value itself; if you also need the name of whoever holds the minimum, use FIRST_VALUE(name) with ORDER BY salary ASC.

Why we use it

Comparing each row to its group's floor is common: how much above the cheapest supplier is each quote, who is the lowest earner per team. MIN() OVER gives that floor on every row in one pass.

When to use it

Use MIN() OVER to compare rows with their group minimum or to track a running minimum. Use FIRST_VALUE when you need another column from the minimum row, and GROUP BY MIN when one row per group is enough.

How it works

  1. Rows split by PARTITION BY
  2. ORDER BY inside OVER? Yes: Running minimum so far. No: Minimum of whole partition.
  3. Ignore NULL values
  4. Write the minimum on every row
  5. Compare, e.g. CASE WHEN salary = dept_min
  6. Rows kept with their group minimum

Syntax

MIN(column) OVER (PARTITION BY col)

Example

SELECT name, department, salary,
  MIN(salary) OVER (PARTITION BY department) AS dept_min
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