• Javascript
  • Python
  • Go

Getting the Minimum Value of Two Fields in Postgres

PostgreSQL, commonly referred to as Postgres, is a powerful and widely used open-source relational database management system. It offers a p...

PostgreSQL, commonly referred to as Postgres, is a powerful and widely used open-source relational database management system. It offers a plethora of features and functionalities that make it a top choice for developers and businesses alike. One of its many useful features is the ability to get the minimum value of two fields in a database table.

In this article, we will explore how to use the MIN() function in Postgres to retrieve the smallest value from two different fields in a table. So, let's dive in and learn how to leverage this functionality in our database queries.

To start off, let's create a sample table named "employees" with the following fields: "id", "name", and "salary". We will insert some dummy data into the table to have something to work with.

```sql

CREATE TABLE employees (

id SERIAL PRIMARY KEY,

name VARCHAR(50),

salary INT

);

INSERT INTO employees (name, salary) VALUES

('John', 50000),

('Jane', 60000),

('Bob', 45000),

('Alice', 70000);

```

Now, suppose we want to find the minimum salary among all the employees in our company. We can use the MIN() function in combination with the SELECT statement to achieve this. The syntax for using the MIN() function is as follows:

```sql

SELECT MIN(column_name) FROM table_name;

```

So, in our case, the query would look like this:

```sql

SELECT MIN(salary) FROM employees;

```

This will return the smallest salary from the "salary" column in the "employees" table, which is 45000. But what if we want to find the minimum value between two fields, say "salary" and "bonus"?

To do that, we can use the MIN() function with the LEAST() function. The LEAST() function takes in multiple parameters and returns the smallest value among them. So, our query would now look like this:

```sql

SELECT LEAST(MIN(salary), MIN(bonus)) FROM employees;

```

This will give us the minimum value between the "salary" and "bonus" fields. If we don't have a "bonus" field in our table, we can also use the MIN() function with the constant value. For example:

```sql

SELECT LEAST(MIN(salary), 50000) FROM employees;

```

This will give us the minimum value between the "salary" field and the constant value of 50000.

Furthermore, we can also use the MIN() function with the GROUP BY clause to find the minimum value of a particular field for each group. For instance, if we want to find the minimum salary for each department in our company, we can use the following query:

```sql

SELECT department, MIN(salary) FROM employees GROUP BY department;

```

This will give us the minimum salary for each department in our company. We can also use the HAVING clause to filter the results based on certain conditions. For example, if we only want to see the departments with a minimum salary greater than 50000, we can use the following query:

```sql

SELECT department, MIN(salary) FROM employees GROUP BY department HAVING MIN(salary) > 50000;

```

In conclusion, the MIN() function in Postgres is a powerful tool that allows us to find the smallest value from one or

Related Articles

Nested IF/IN CASE query in Postgres

PostgreSQL is a widely used open-source relational database management system (RDBMS) known for its robustness and scalability. It offers a ...