PostgreSQL: MIN and MAX Functions – Finding Minimum and Maximum Values

Welcome to The Coding College, where we simplify coding concepts for learners! In this tutorial, we’ll explore PostgreSQL’s MIN and MAX functions, which are essential for finding the smallest and largest values in a dataset.

What are MIN and MAX Functions?

  • MIN: Returns the smallest value in a specified column.
  • MAX: Returns the largest value in a specified column.

These functions work on numeric, date, and even text data types in PostgreSQL.

Syntax

SELECT MIN(column_name) AS minimum_value, MAX(column_name) AS maximum_value
FROM table_name
[WHERE condition];
  • column_name: The column you want to evaluate.
  • AS: Provides an alias for the returned value.
  • WHERE: (Optional) Filters rows before applying the function.

Example: Sample Table

Consider a table named products:

product_idproduct_namepricecategory
1Laptop75000Electronics
2Smartphone45000Electronics
3Desk15000Furniture
4Chair8000Furniture
5TV55000Electronics

Using MIN and MAX

Example 1: Find the Minimum and Maximum Prices

SELECT MIN(price) AS minimum_price, MAX(price) AS maximum_price
FROM products;

Result:

minimum_pricemaximum_price
800075000

Using MIN and MAX with WHERE Clause

Example 2: Find the Price Range for Electronics

SELECT MIN(price) AS minimum_price, MAX(price) AS maximum_price
FROM products
WHERE category = 'Electronics';

Result:

minimum_pricemaximum_price
4500075000

Combining with Other Columns

Example 3: Find the Product with the Maximum Price

SELECT product_name, price
FROM products
WHERE price = (SELECT MAX(price) FROM products);

Result:

product_nameprice
Laptop75000

Example 4: Find the Product with the Minimum Price

SELECT product_name, price
FROM products
WHERE price = (SELECT MIN(price) FROM products);

Result:

product_nameprice
Chair8000

Aggregating with Groups

You can use MIN and MAX with the GROUP BY clause to find the minimum and maximum values for each group.

Example 5: Find the Minimum and Maximum Price by Category

SELECT category, MIN(price) AS minimum_price, MAX(price) AS maximum_price
FROM products
GROUP BY category;

Result:

categoryminimum_pricemaximum_price
Electronics4500075000
Furniture800015000

Real-World Applications

  1. Price Analysis: Identify the cheapest and most expensive items in your store.
  2. Performance Metrics: Find the best and worst performers in an organization.
  3. Date Ranges: Determine the earliest and latest dates in a dataset.

Learn More at The Coding College

At The Coding College, we focus on user-friendly tutorials designed to enhance your learning experience. We adhere to Google’s E-E-A-T principles, ensuring expert and accurate content for coding enthusiasts.

Conclusion

PostgreSQL’s MIN and MAX functions are simple yet powerful tools for data analysis. Whether you’re working with prices, dates, or any other values, these functions help you extract key insights quickly and efficiently.

Stay tuned to The Coding College for more PostgreSQL tutorials and other programming tips!

Leave a Comment