EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL MIN()

PostgreSQL MIN()

Sohel Sayyad
Article bySohel Sayyad
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated May 18, 2023

PostgreSQL MIN()

Introduction to PostgreSQL MIN() Function

PostgreSQL supports various aggregate functions. The PostgreSQL MIN function is one of them, which is used to return the minimum value from the set of record values. We can use the PostgreSQL MIN function to find the minimum value for the column within the table. We need to pass the name of the column to the PostgreSQL MIN function specified in the SELECT SQL statement. The column we can pass to the PostgreSQL MIN function can have the data types such as string, number, or any other data type which can be compared. We can use the PostgreSQL MIN function with various clauses as well as with other aggregate functions as well.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

MIN (* | [DISTINCT] ALL | column_name)

Explanation:

  • *: This shows all of the rows.
  • column_name: This specifies the name of the column.
  • DISTINCT: It is used to show uniqueness and is an optional clause.
  • ALL: It specifies the default clause, and it is optional.

How does PostgreSQL MIN() Function Work?

  • The PostgreSQL MIN function is used for finding the minimum value for the specific column within the table, and it takes the name of the column as a parameter.
  • The column we can pass to the PostgreSQL MIN function can have the data types such as string, number, or any other data type which can be compared.
  • We can use the PostgreSQL MIN function with various clauses as well as other aggregate functions as well.

Examples

Let’s create a table named ‘furniture’ in order to understand the examples:

CREATE table furniture
(
furniture_id SERIAL PRIMARY KEY,
furniture_name VARCHAR (256) NOT null,
furniture_type VARCHAR (256) NOT null,
furniture_price int NULL
);

Now, insert some data in the furniture table in order to execute SQL statements.

INSERT INTO furniture (furniture_name,furniture_type,furniture_price)
VALUES
('Chair','Wood',2500),
('Chair','Plastic',2000),
('Chair','Rexine',3000),
('Table','Wood',5000),
('Table','Plastic',4000),
('Table','Rexine',5500),
('Sofa','Wood',10000),
('Sofa','Plastic',8000),
('Sofa','Rexine',8500),
('Bed','Wood',15000),
('Bed','Plastic',13000),
('Bed','Rexine',14000);

Illustrate the result of the above statement with the help of the following snapshot and the SELECT statement.

SELECT * FROM furniture;

Furniture Table

Example #1 – The PostgreSQL MIN() Function as a Level

Consider the following examples, which will show how to get the furniture with the minimum price from a table named ‘furniture’.

We can achieve this by writing a SQL statement in different ways as follows:

SELECT MIN(furniture_price) "Minimum price furniture"
FROM furniture;

Illustrate the result of the above statement by using the following snapshot:

Output-2

SELECT MIN(DISTINCT furniture_price) "Minimum price furniture"
FROM furniture;

Illustrate the result of the above statement by using the following snapshot:

Output - 3

SELECT MIN(ALL furniture_price) "Minimum price furniture"
FROM furniture;

Use the image below to demonstrate the outcome of the above statement:

PostgreSQL MIN()-2

Example #2 – WHERE Clause with the MIN() Function

Consider the following example, which will show how to get the furniture with the minimum price from the table named ‘furniture’ where the name of the furniture is Chair.

SELECT MIN(furniture_price) "Minimum price furniture"
FROM furniture6
WHERE furniture_name='Chair';

Use the image below to demonstrate the outcome of the above statement:

PostgreSQL MIN()-2

Example #3 – GROUP BY with the MIN() Function

Consider the following example for getting the minimum furniture price for each furniture type from the furniture table.

SELECT furniture_type, MIN(furniture_price) "Minimum price furniture"
FROM furniture
GROUP BY furniture_type;

Use the image below to demonstrate the outcome of the above statement:

PostgreSQL MIN()-3

Example #4 – HAVING CLAUSE with the MIN() Function

Consider the following example for getting the minimum furniture price for each furniture type whose minimum price is 2500 and below 13000 in the furniture table.

SELECT furniture_type, MIN(furniture_price) "Minimum price furniture"
FROM furniture
WHERE furniture_price < 13000
GROUP BY furniture_type
HAVING MIN(furniture_price) >= 2500;

Use the image below to demonstrate the outcome of the above statement:

PostgreSQL MIN()-4

Example #5 – GROUP BY and ORDER BY with the MIN() Function

Consider the following example for getting the furniture with a minimum furniture price is 2500 and below 13000 and the minimum furniture price for each furniture type as in descending order.

SELECT furniture_type, MIN(furniture_price) "Minimum price furniture"
FROM furniture
WHERE furniture_price < 13000
GROUP BY furniture_type
HAVING MIN(furniture_price) >= 2500
ORDER BY MIN(furniture_price) DESC;

Illustrate the result of the above statement by using the following snapshot:

PostgreSQL MIN()-5

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL MIN()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL Variables
  2. PostgreSQL GROUP BY
  3. Trunc() PostgreSQL
  4. PostgreSQL cluster
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle2000+ Hour of HD Videos | 80 Learning Paths | 400+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more