PostgreSQL MIN function
MIN function
The MIN function determines the smallest of all selected values of a column.
Syntax:
MIN (* | [DISTINCT] ALL | column_name)
Parameters
Name | Description |
---|---|
column_name | Name of the column |
* | The asterisk(*) indicates all the rows. |
DISTINCT | This clause is optional. It indicates uniqueness. |
ALL | This clause is optional. It is default clause. |
Contents:
PostgreSQL MIN function example
The sample table
If we want to find the minimum salary from all employees in the employee table, the following SQL can be used.
SQL Code:
SELECT MIN(salary)
FROM employee;
Output:
![postgresql min function example1](https://www.w3resource.com/w3r_images/postgresql-min-function-example1.gif)
Visual Presentation of PostgreSQL MIN()
PostgreSQL MIN as a level
If we want to get the minimum salary for all employees and show the result against 'Minimum Salary' head in the employee table, the following SQL can be used.
SQL Code:
SELECT MIN(salary) "Minimum Salary"
FROM employee;
OR
SQL Code:
SELECT MIN(DISTINCT salary) "Minimum Salary"
FROM employee;
OR
SQL Code:
SELECT MIN(ALL salary) "Minimum Salary"
FROM employee;
Output:
![postgresql min function as a level name](https://www.w3resource.com/w3r_images/postgresql-min-function-as-level.gif)
PostgreSQL MIN function two columns
If we want to get the minimum salary and deduction from employee table, the following SQL can be used.
SQL Code:
SELECT MIN(salary) "Minimum Salary" ,
MIN(deduction) "Minimum Deduction"
FROM employee;
Output:
![postgresql min function two columns](https://www.w3resource.com/w3r_images/postgresql-min-function-two-columns.gif)
PostgreSQL MIN with MAX, COUNT, and SUM
If we want to get the minimum salary and deduction from employee table, the following SQL can be used.
SQL Code:
SELECT MIN(salary) "Minimum Salary" ,
MAX(salary) "Maximum Salary",
COUNT(*) "Total Employee",
SUM(salary) "Total Salary"
FROM employee;
Output:
![postgresql min function with max,count and sum](https://www.w3resource.com/w3r_images/postgresql-min-function-max-count-sum.gif)
PostgreSQL MIN WHERE clause
If we want to get the minimum salary and deduction from employee table who belongs to the designation 'SALESMAN', the following SQL can be used.
SQL Code:
SELECT MIN(salary) "Minimum Salary" ,
MIN(deduction) "Minimum Deduction"
FROM employee
WHERE designame='SALESMAN';
Output:
![postgresql min function where clause](https://www.w3resource.com/w3r_images/postgresql-min-function-where-clause.gif)
PostgreSQL MIN with GROUP BY
Sample table: employees
If we want to get the minimum salary for each designation available in employees table, the following SQL can be used.
SQL Code:
SELECT job_id, MIN(salary) AS "Minimum Salary"
FROM employees
GROUP BY job_id;
Output:
job_id | Minimum Salary ------------+--------------- AC_ACCOUNT | 8300.00 ST_MAN | 5800.00 IT_PROG | 4200.00 SA_MAN | 10500.00 AD_PRES | 24000.00 AC_MGR | 12000.00 FI_MGR | 12000.00 AD_ASST | 4400.00 MK_MAN | 13000.00 PU_CLERK | 2500.00 HR_REP | 6500.00 PR_REP | 10000.00 FI_ACCOUNT | 6900.00 SH_CLERK | 2500.00 AD_VP | 17000.00 SA_REP | 6100.00 ST_CLERK | 2100.00 MK_REP | 6000.00 PU_MAN | 11000.00 (19 rows)
Visual Presentation of PostgreSQL MIN with GROUP BY
PostgreSQL MIN with HAVING CLAUSE
Sample table: employees
If we want to get those designations, whose minimum salary is 6500 and above within the salary range below 12000, the following SQL can be used.
SQL Code:
SELECT job_id,MIN(salary) AS "Minimum Salary"
FROM employees
WHERE salary<12000
GROUP BY job_id
HAVING MIN(salary)>=6500;
Output:
job_id | Minimum Salary ------------+---------------- AC_ACCOUNT | 8300.00 SA_MAN | 10500.00 HR_REP | 6500.00 PR_REP | 10000.00 FI_ACCOUNT | 6900.00 PU_MAN | 11000.00 (6 rows)
Visual Presentation of PostgreSQL MIN with HAVING
PostgreSQL MIN with GROUP BY and ORDER BY
Sample table: employees
The following query will return the designation which minimum salary is 6500 and above within the salary range below 12000 and the minimum salary for each designation comes in the list in descending order.
SQL Code:
SELECT job_id,MIN(salary) AS "Minimum Salary"
FROM employees
WHERE salary<12000
GROUP BY job_id
HAVING MIN(salary)>=6500
ORDER BY MIN(salary) DESC;
Output:
job_id | Minimum Salary ------------+---------------- PU_MAN | 11000.00 SA_MAN | 10500.00 PR_REP | 10000.00 AC_ACCOUNT | 8300.00 FI_ACCOUNT | 6900.00 HR_REP | 6500.00 (6 rows)
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics