Pathubs SQL Curriculum • Module 11

SQL MIN / MAX

Master finding boundaries and extremes: learn how MIN() and MAX() locate smallest numbers, earliest dates, and latest timestamps, understand NULL exclusion, and compare them with ORDER BY ... LIMIT.

⏱️ Estimated Time:45 Minutes
🎯 Level:Beginner
📊 Track:Data Analytics & SQL Mastery
✨ Mode:Interactive Boundary & Extreme Value Lab
1

Introduction

In data analysis and business intelligence, identifying the extreme boundaries of a dataset is a daily necessity. MIN and MAX answer two fundamental questions:

  • MIN: What is the smallest, lowest, or earliest value? (e.g. lowest product price, earliest order date, minimum test score).
  • MAX: What is the largest, highest, or latest value? (e.g. highest employee salary, latest customer sign-up, peak server load).
2

What Are MIN and MAX?

MIN() and MAX() are aggregate functions built into all mainstream SQL engines. They scan a set of values and return the single extreme scalar result:

SELECT MIN(price) FROM products;
SELECT MAX(price) FROM products;
3

Basic Syntax

MIN(expression)
MAX(expression)

The expression is usually a column name (such as price or created_at), but it can also be an arithmetic calculation.

4

Your First MIN Query

Given product prices: [100, 250, 80, 400]:

SELECT MIN(price) AS lowest_price
FROM products;

Result: 80.

Diagram 1: MIN() Extreme Flow
Values: 100, 250, 80, 400
➔ MIN() ➔
Smallest = 80
5

Your First MAX Query

Using the same dataset [100, 250, 80, 400]:

SELECT MAX(price) AS highest_price
FROM products;

Result: 400.

Diagram 2: MAX() Extreme Flow
Values: 100, 250, 80, 400
➔ MAX() ➔
Largest = 400
6

MIN / MAX With WHERE

When paired with a WHERE clause, the database filters the candidate rows first before locating the extreme value:

SELECT MAX(salary) AS top_sales_salary
FROM employees
WHERE department = 'Sales';

SELECT MIN(price) AS cheapest_electronics
FROM products
WHERE category = 'Electronics';
🧠
Mental Model: TableWHERE filters rowsMatching valuesMIN / MAXSingle Result.
7

MIN / MAX and NULL Rules

Like all standard aggregate functions, MIN and MAX completely ignore NULL values.

Given prices [100, NULL, 250, 80, NULL]:

  • NULLs are discarded: only [100, 250, 80] are evaluated.
  • MIN() is 80 (NULL is NOT treated as 0).
  • MAX() is 250.
⚠️
Edge Case: If all rows contain NULL or if 0 rows match the WHERE filter, MIN() and MAX() return NULL.
Diagram 3: NULL Ignored in Boundary Finding
Values: 100, NULL, 250, 80
Ignore NULL ➔ [100, 250, 80]
➔ Evaluate ➔
MIN = 80 & MAX = 250
8

MIN / MAX With Duplicate Values

Duplicate values have zero impact on the final minimum or maximum.

For example, with values [100, 100, 250, 80, 80]:

  • MIN() is still 80.
  • MAX() is still 250.
9

MIN / MAX With Dates & Timestamps

MIN and MAX work seamlessly on chronological date and timestamp columns:

SELECT MIN(order_date) AS first_order_date,
       MAX(order_date) AS latest_order_date
FROM orders;
  • MIN(order_date): Returns the earliest / oldest date in the records.
  • MAX(order_date): Returns the latest / most recent date in the records.
10

MIN / MAX With Text Columns

SQL systems (such as PostgreSQL and MySQL) also support MIN and MAX on text strings using alphabetical collation ordering:

SELECT MIN(name), MAX(name) FROM customers;

MIN(name) returns the name that comes first alphabetically (e.g. 'Aarav'), while MAX(name)returns the name that comes last (e.g. 'Zara').

11

MIN / MAX vs ORDER BY + LIMIT

Beginners often wonder how MIN(price) differs from sorting and taking the top row:

Aggregate Function: MIN(price)
SELECT MIN(price)
FROM products;

Returns a single scalar aggregate value. If table is empty, returns 1 row with NULL.

Row Stream: ORDER BY + LIMIT
SELECT price
FROM products
ORDER BY price ASC
LIMIT 1;

Sorts candidate rows and limits the output rows. If table is empty, returns 0 rows.

Live Interactive MIN / MAX Lab
📦 Products Catalog & PricesVisual Extreme Finder
#ProductCategoryPriceExtreme Status
1Wireless MouseElectronics₹250Valid non-NULL value
2Cotton T-ShirtClothing₹100Valid non-NULL value
3Mechanical KeyboardElectronics₹800Valid non-NULL value
4Clearance USB CableElectronics₹80⭐ Minimum Value
5Denim JeansClothing₹250Valid non-NULL value
64K Ultra Gaming MonitorElectronics₹2,500Valid non-NULL value
7Custom Prototype HubElectronicsNULLNULL (Ignored)
8Basic Crew SocksClothing₹80⭐ Minimum Value
9Specialty Winter JacketClothingNULLNULL (Ignored)
✍️ SQL Extreme Query● Scalar Output
SELECT MIN(price) AS lowest_price
FROM products;
₹80
Output column: lowest_price
Evaluation: MIN of [₹250, ₹100, ₹800, ₹80, ₹250, ₹2500, ₹80] ➔ ₹80
12

Common MIN / MAX Mistakes

1. Assuming MIN/MAX Returns the Whole Row

SELECT MAX(salary) FROM employees; returns only the scalar number (e.g. ₹90,000), not the employee name who earns it.

2. Treating NULL as Zero

MIN() does NOT treat NULL as zero. With values [100, NULL, 50], MIN returns 50, not 0.

3. Confusing MIN/MAX with COUNT

COUNT counts the rows; MIN and MAX find the lowest and highest values in those rows.

🧠 Interactive Challenge: Predict The Result (1 of 3)
SELECT MIN(price) FROM products;
Given prices [100, NULL, 250, 80, 250], what will MIN(price) return?

MIN vs MAX Scenario Challenge

Test your intuition: choose whether you would use MIN or MAX for each real-world analytics scenario:

Finding the lowest product price in your catalog
Column context: Price column (e.g. ₹80 to ₹2500)
Identifying the highest salary in the Sales department
Column context: Salary column (e.g. ₹45k to ₹120k)
Discovering the earliest customer registration date
Column context: Created_at date column (e.g. 2021-01-10 to 2026-08-20)
Finding the latest shipment update timestamp
Column context: Updated_at timestamp column
13

Practical Step-by-Step Exercises

Task GoalTarget TableRequired SQL SolutionFunction Pattern
1. Lowest Product PriceproductsSELECT MIN(price) FROM products;Table-level MIN
2. Highest Product PriceproductsSELECT MAX(price) FROM products;Table-level MAX
3. Earliest Order DateordersSELECT MIN(order_date) FROM orders;Date MIN
4. Latest Order DateordersSELECT MAX(order_date) FROM orders;Date MAX
5. Top Sales SalaryemployeesSELECT MAX(salary) FROM employees WHERE department = 'Sales';WHERE + MAX
14

MIN / MAX Best Practices

  • Always Alias Your Extremes: Write SELECT MIN(price) AS lowest_price for intuitive reporting headers.
  • Filter Before Aggregating: Narrow your dataset using WHERE clauses for specific categories or date spans.
  • Remember That NULL is Excluded: Never assume NULL is treated as 0 or the lowest value.
  • Do Not Expect Row IDs: If you need the full row with the minimum or maximum value, use subqueries or ordering techniques.
15

What You Should Know Now

  • MIN(col): Finds smallest number, earliest date, or first text
  • MAX(col): Finds largest number, latest date, or last text
  • NULL Handling: NULL values are ignored during evaluation
  • Empty Sets: Returns NULL when 0 valid rows match
  • Duplicates: Duplicates do not alter the minimum or maximum
  • Scalar Output: Returns a single boundary value, not the whole row

🎯 Knowledge Check Quiz: SQL MIN / MAX

Test your understanding of boundary aggregations, NULL rules, date handling, duplicates, and row isolation.

1. What is the primary role of the SQL MIN() aggregate function?
2. What is the primary role of the SQL MAX() aggregate function?
3. How do MIN() and MAX() handle NULL values in a column?
4. Given the list of values [100, 100, 250, 80, 80], how do the duplicate values affect the MIN and MAX?
5. When applied to DATE or TIMESTAMP columns, what do MIN(date) and MAX(date) represent?
6. Does the query `SELECT MAX(salary) FROM employees;` return the name of the employee who earns that salary?
7. What is a key difference between `SELECT MIN(price) FROM products;` and `SELECT price FROM products ORDER BY price ASC LIMIT 1;`?
8. If a table has 5 rows and all 5 rows have NULL in the target column, what will `SELECT MAX(column) FROM table;` return?