Chapter 23: PostgreSQL MIN and MAX Functions

Part 1: What are MIN and MAX?

MIN returns the minimum (smallest) value in a set of values.
MAX returns the maximum (largest) value in a set of values.

These are aggregate functions – they operate on a set of rows and return a single value.

The Basic Syntax

sql

You can use them together in a single query:

sql

Part 2: Setting Up Our Example Data

Let’s create a comprehensive dataset to explore all the ways MIN and MAX can be used:

sql


Part 3: Basic MIN and MAX Usage

1. Finding the Minimum Value

sql

Result:

cheapest_price
9.99

2. Finding the Maximum Value

sql

Result:

most_expensive_price
499.99

3. Getting Both Extremes Together

sql

Result:

min_price max_price price_range
9.99 499.99 490.00

4. MIN and MAX on Different Data Types

With Dates:

sql

Result:

oldest_product newest_product
2020-06-01 2024-02-01

With Text (Alphabetical):

sql

Result:

first_alphabetically last_alphabetically
1984 Yoga Mat

Notice: Numbers come before letters in ASCII sorting, so “1984” is first.

Part 4: MIN and MAX with WHERE Clauses

1. Filtered Extremes

sql

Result:

most_expensive_electronics
399.99

2. Multiple Conditions

sql

Result:

cheapest_book
9.99

3. Date Ranges with MIN/MAX

sql

Part 5: MIN and MAX with GROUP BY

This is where MIN and MAX become truly powerful – finding extremes within each group.

1. Minimum and Maximum per Category

sql

Result:

category min_price max_price price_range
Furniture 45.99 499.99 454.00
Electronics 39.99 399.99 360.00
Appliances 34.99 199.99 165.00
Sports 14.99 89.99 75.00
Books 9.99 14.99 5.00
Misc 0.00 0.00 0.00

2. Multiple Aggregates with MIN/MAX

sql

3. Finding Oldest and Newest Products by Category

sql

Part 6: MIN and MAX with HAVING

Filter groups based on their min or max values:

1. Categories with Expensive Products

sql

Result:

category max_price product_count
Furniture 499.99 5
Electronics 399.99 5
Appliances 199.99 5

2. Categories with Narrow Price Ranges

sql

Result:

category min_price max_price price_range
Books 9.99 14.99 5.00
Misc 0.00 0.00 0.00

3. Categories with Recent Activity

sql

Part 7: MIN and MAX with Joins

Combine MIN/MAX with related tables for deeper insights:

1. Best and Worst Selling Products

sql

For accurate product names, we need subqueries (coming in Part 9).

2. Customer Order Statistics

sql

Result:

customer_name first_order last_order customer_lifetime_days
Oscar Wilde 2024-02-04 2024-02-04 0
Nina Simone 2024-02-02 2024-02-02 0
Mona Lisa 2024-02-05 2024-02-05 0

3. Product Performance by Category

sql

Part 8: MIN and MAX with NULL Values

NULL handling is crucial with aggregate functions:

1. How NULLs Affect MIN and MAX

sql

Important: MIN and MAX ignore NULL values. They don’t return NULL unless all values are NULL.

2. All NULLs Case

sql

3. Handling NULLs Explicitly

sql

Part 9: Finding the Row with MIN or MAX Value

A common need: find the entire row that contains the minimum or maximum value.

1. Using Subqueries (The Classic Way)

sql

Result:

product_id name category price
12 Standing Desk Furniture 499.99

2. Handling Ties

sql

3. Using ORDER BY with LIMIT (Simpler but Returns Only One)

sql

4. For Each Category – The Most Expensive Product

sql

Better with Window Functions:

sql

Result:

category name price
Appliances Microwave 199.99
Books The Hobbit 14.99
Electronics 4K Monitor 399.99
Furniture Standing Desk 499.99
Misc Sample Item 0.00
Sports Running Shoes 89.99

5. Using DISTINCT ON (PostgreSQL-specific)

sql

Part 10: Real-World Examples

Example 1: Sales Dashboard

sql

Example 2: Inventory Management

sql

Example 3: Customer Lifetime Value Analysis

sql

Example 4: Price Range Analysis for Marketing

sql

Example 5: Seasonal Trends

sql

Part 11: Performance Considerations

1. Indexes for MIN and MAX

Indexes dramatically speed up MIN and MAX queries:

sql

2. How PostgreSQL Optimizes MIN/MAX

With an index, PostgreSQL can get the minimum by reading just the first entry in the index:

sql

Look for “Index Only Scan” or “Limit” in the plan – it might show that PostgreSQL only needed to read one index entry!

3. MIN/MAX on Grouped Queries

For grouped queries, a composite index on (category, price) helps:

sql

Part 12: Common Mistakes and How to Avoid Them

Mistake 1: Forgetting NULL Handling

sql

Mistake 2: Using MIN/MAX on Text for Dates

sql

Mistake 3: Assuming MIN/MAX Return the Whole Row

sql

Mistake 4: Not Considering Ties

sql

Mistake 5: Using MIN/MAX on Non-Indexed Columns in Large Tables

sql

Summary: The MIN and MAX Philosophy

MIN and MAX are your window into the boundaries of your data. Master these concepts:

  1. Basic usage – Find smallest and largest values in any column

  2. With GROUP BY – Find extremes within categories

  3. With HAVING – Filter groups based on their extremes

  4. With subqueries – Find the actual rows containing extremes

  5. Performance – Indexes make MIN/MAX lightning fast

Remember:

  • MIN and MAX ignore NULL values

  • They work on numbers, dates, text – any orderable data type

  • For text, they use alphabetical order

  • Combine with GROUP BY for per-category analysis

  • Use subqueries or window functions to get full rows

Key applications:

  • Price ranges (“What’s our cheapest and most expensive product?”)

  • Date ranges (“When was our first and last order?”)

  • Performance boundaries (“Who are our highest and lowest spenders?”)

  • Quality control (“What are the best and worst rated items?”)

  • Inventory management (“Which products have the most and least stock?”)

The MIN and MAX functions transform your database from a collection of data into a source of insights about boundaries, extremes, and ranges. They answer questions like “How bad is the worst case?” and “How good is the best case?” – essential for any data analysis.

Would you like me to elaborate on any specific aspect of MIN and MAX, such as more complex use cases, performance optimization, or integration with other aggregate functions?

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *