How to Use the PostgreSQL Min Function in Windows 10?
We have already stated that the PostgreSQL Min function calculates the lowest value out of the provided input values. It does so by scanning all the input values provided to this function and then presenting the minimum of these values to you. We normally pass a column or columns of a PostgreSQL table to the Min function as an input. Moreover, apart from this, the Min function of PostgreSQL can also be combined with some other clauses and statements of PostgreSQL to formulate more complex working scenarios. The example described below will thoroughly walk you through the usage of the Min function of PostgreSQL in Windows 10.
Example: Finding the Minimum Value from a Column of a PostgreSQL Table in Windows 10
We have created a thorough example for you in which we are going to extract the minimum value from a column of a PostgreSQL table in Windows 10 in different ways. For that, we will first create a PostgreSQL table and populate it with some random values. After that, we will try to find out the minimum value from a particular column of that PostgreSQL table by using different techniques. Let us read through this example together to determine how the Min function works in PostgreSQL in Windows 10.
Step 1: Creation of a Sample Table in PostgreSQL
Since we are going to extract the minimum value from the column of a PostgreSQL table, therefore, we need to create a sample table first with the help of the following query:
We have attempted to create a table named “meal” with three attributes, namely: Dish_Name, Dish_Type, and Dish_Price. The PostgreSQL table creation can be confirmed through the response shown below:
Step 2: Populating the Sample Table in PostgreSQL
After creating this table, we want to insert some records into this table to run queries on this data later on. We will execute the following command for inserting a few records into the “meal” table:
If you execute the above-mentioned query without making any mistakes, then you will be able to insert 9 records successfully into the “meal” table, as shown in the image below. You can even insert more records if you want to.
Step 3: Viewing the Records of the Sample PostgreSQL Table
To test our record insertion into the “meal” table, we will run the “SELECT” query as shown in the following image:
Step 4: Finding the Minimum Value from one of the Columns of the Sample PostgreSQL Table using the “Min” Function
Now, we want to find out the smallest value from one of the PostgreSQL table “meal” columns that we have just created. For that, we are going to use the Min function of PostgreSQL as depicted by the query stated below:
With the help of the above-mentioned query, we just want to find out the lowest dish price. For that, we have passed the “Dish_Price” column of the “meal” table as an argument to the Min function of PostgreSQL.
The minimum dish price from the “Dish_Price” column of the meal table is shown in the following image:
Step 5: Finding the Minimum Value from one of the Columns of the Sample PostgreSQL Table while using the “WHERE” Clause with the “Min” Function
Now, we will make this example a bit more complex and introduce the “WHERE” clause in our PostgreSQL query along with the Min function. We want to restrict the minimum price to a specific dish type. It means that we do not want to scan the whole “Dish_Price” column to find the lowest price; rather, we just want to find the lowest price according to a specific “Dish_Type”. This target can be achieved by executing the query shown below:
In this query, we want to find out the lowest price from the “meal” table but only for the starters. For that, we have used the “WHERE” clause in PostgreSQL to specify that we only want the lowest price for the starters.
The lowest price for the starters is shown in the following image:
Step 6: Finding the Minimum Value from one of the Columns of the Sample PostgreSQL Table while using the “GROUP BY” Clause with the “Min” Function
Finally, we will make the same example all the more complex. We will try to find out the lowest prices for all the three dish types, i.e., Main Course, Starter, and Dessert. For that, we will use the “GROUP BY” clause of PostgreSQL that groups the output according to the specified attribute. The query that we will execute to achieve this goal is shown below:
In this query, we want to display the minimum dish price on the console and the dish type to which this price belongs. For that, we have applied the Min function of PostgreSQL on the “Dish_Price” whereas we have grouped the results by the “Dish_Type” to get the lowest prices belonging to all of the three dish types.
The lowest prices for all of our three dish types are shown in the following image:
The main motive of this article was to talk about the usage of the Min function of PostgreSQL in Windows 10. We first described that this function belongs to the aggregate functions of PostgreSQL and is used to find the minimum value out of all the provided inputs to this function. Finally, to elaborate more on the usage of this function, we formulated an example scenario in which we explained to you the simple, the moderate, and the complex usage of the Min function by combining it with other PostgreSQL clauses. Once you get a good command of this usage, you will also be able to combine this function with other PostgreSQL clauses such as “ORDER BY” and PostgreSQL joins.