PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Functions / PostgreSQL MIN Function

PostgreSQL MIN Function

Summary: in this tutorial, you will learn how to use PostgreSQL MIN function to get the minimum value of a set.

Introduction to PostgreSQL MIN function

PostgreSQL MIN function is one of aggregate functions that returns the minimum value in a set of values.

To find the minimum column value in a table, you pass that column to the MIN function in the  SELECT statement. The data type of the column can be number, string, or any comparable type.

The syntax of the MIN function is as follows:

1
2
SELECT MIN(column)
FROM table;

PostgreSQL MIN function examples

Let’s take a look at some examples of using the  MIN function.

PostgreSQL MIN function in SELECT clause

We will use the film table in the dvdrental sample database for demonstration.

Film Table

You often use the  MIN function in the  SELECT clause. For example, to get the minimum rental rate of the film in the film table, you use the following query:

1
2
3
4
SELECT
   MIN (rental_rate)
FROM
   film;

PostgreSQL MIN Function

The query returns 0.99, which is the minimum rental rate.

PostgreSQL MIN function in a subquery

To get films which have the minimum rental rate, you use the following query:

1
2
3
4
5
6
7
8
9
10
11
12
13
SELECT
film_id,
title,
rental_rate
FROM
film
WHERE
rental_rate = (
SELECT
MIN (rental_rate)
FROM
film
);

PostgreSQL MIN Subquery

In this query, we used a subquery to select the minimum rental rate. In the outer query, we selected all films which have rental rate equals to the minimum rental rate returned from the subquery. The following diagram illustrates the steps that PostgreSQL performed the query:

postgresql min function explain

PostgreSQL MIN function with GROUP BY clause

You can use the  MIN function in the query that uses GROUP BY clause to find the minimum value per group for all groups. For example, for each group of films that have the same rating, the following query finds the minimum rental rate.

1
2
3
4
5
6
7
8
9
SELECT
rating,
MIN (rental_rate)
FROM
film
GROUP BY
rating
ORDER BY
rating;

PostgreSQL MIN GROUP BY

PostgreSQL MIN function group by

PostgreSQL MIN function with HAVING clause

You can use the MIN function not only in the SELECT clause but also in the HAVING clause the filter the groups whose minimum values match a certain condition.

For example, the following query finds the minimum replacement costs of films grouped by rating and selects only rating that has replacement cost greater than 9.

1
2
3
4
5
6
7
8
SELECT
rating,
MIN (replacement_cost)
FROM
film
GROUP BY
rating
HAVING MIN (replacement_cost) > 9

PostgreSQL MIN function with other aggregate functions

You can combine the  MIN function with the MAX function in the same query to find the minimum and maximum values per group. For example, for each group of films that have the same rating, the following query selects the minimum and maximum rental rates.

For example, for each group of films that have the same rating, the following query selects the minimum and maximum rental rates.

1
2
3
4
5
6
7
8
SELECT
rating,
MIN (rental_rate),
        MAX (rental_rate)
FROM
film
GROUP BY
rating;

PostgreSQL MIN MAX

Finding smallest values from two or more columns

Suppose, you have the following ranks table:

1
2
3
4
5
6
CREATE TABLE ranks (
user_id INT PRIMARY KEY,
rank_1 int4 NOT NULL,
rank_2 int4 NOT NULL,
rank_3 int4 NOT NULL
);

And its sample data:

1
2
3
4
5
INSERT INTO ranks
VALUES
(1, 6, 3, 5),
(2, 2, 8, 5),
(3, 5, 9, 8);

Now the requirement is that for each user, find its smallest possible rank as follows:

postgresql least function

You cannot use the MIN function because the MIN function is applied to rows, not columns. To do this, you use the LEAST function that returns the smallest value from a list of values.

1
2
3
4
5
SELECT
user_id,
LEAST (rank_1, rank_2, rank_3) AS smallest_rank
FROM
ranks;

The query returns the result as we expected.

In this tutorial, we have shown you how to use the PostgreSQL MIN function get minimum value in a set of values.

Related Tutorials

  • PostgreSQL MAX Function
  • PostgreSQL AVG Function
  • PostgreSQL SUM Function
  • PostgreSQL COUNT Function
Previous Tutorial: PostgreSQL MAX Function
Next Tutorial: PostgreSQL SUM Function

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Aggregate Functions

  • PostgreSQL COUNT Function
  • PostgreSQL AVG Function
  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL SUM Function

PostgreSQL Conditional Expressions

  • PostgreSQL CASE
  • PostgreSQL NULLIF
  • PostgreSQL COALESCE

PostgreSQL String functions

  • PostgreSQL CONCAT Function
  • PostgreSQL TRIM Function
  • PostgreSQL LENGTH Function
  • PostgreSQL REPLACE
  • PostgreSQL Substring
  • PostgreSQL Letter Case Functions

PostgreSQL Operators

  • PostgreSQL CAST

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.