Open In App

PostgreSQL MIN() Function

Last Updated : 22 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The MIN() function in PostgreSQL is an essential aggregate function that returns the minimum value in a set of values. This function is highly useful in various data analysis and reporting tasks, allowing you to quickly identify the smallest values in your datasets.

Let us better understand the MIN() Function in PostgreSQL to better understand the concept.

Syntax

MIN(expression)

Where ‘expression' represents the column or expression from which you want to find the minimum value.

PostgreSQL MIN() Function Examples

Let us take a look at some of the examples of the MIN() Function in PostgreSQL to better understand the concept. For example, we will be using the sample database (ie, dvdrental).

Example 1: Finding the Minimum Payment Amount

The following query retrieves the smallest payment amount recorded in the ‘payment' table.

Query:

SELECT MIN(amount)
FROM payment;

Output:

PostgreSQL MIN() Function Example

Explanation: This query scans the ‘amount' column in the ‘payment' table and returns the minimum value, which represents the lowest payment made by any customer.

Example 2: Minimum Payment Per Customer

The next example shows how to find the smallest payment made by each customer. This involves grouping the results by ‘customer_id'.

Query:

SELECT
customer_id,
MIN(amount)
FROM
payment
GROUP BY
customer_id;

Output:

PostgreSQL MIN() Function Example

Explanation: In this query, ‘customer_id' is used to group the payments. The MIN(amount) function finds the smallest payment for each customer within these groups.

Important Points About PostgreSQL MIN() Function

  • The MIN() function computes the minimum value over a specified set of values, which can be an entire table or a subset defined by a WHERE clause.
  • MIN() can be effectively combined with GROUP BY to find minimum values within groups, and with HAVING to filter groups based on aggregate conditions.
  • Although MIN() operates on a single column, you can include multiple MIN() functions in a single query to find minimum values of different columns.
  • MIN() ignores NULL values in the computation.

Next Article

Similar Reads

three90RightbarBannerImg