La funzione SQL Min() è una funzione aggregata inSQL. SQL Min() restituisce il valore minimo di una determinata condizione. L'espressione può essere numerica o può essere un'espressione.
La sintassi per la funzione select min:
SELECT MIN(column_name) FROM table_name WHERE conditions;
Diamo un'occhiata a SQL SELECT MIN.
Considera le tabelle esistenti, che hanno i seguenti record:
Tabella:Dipendenti
EMPLOYEEID | FIRST_NAME | COGNOME | STIPENDIO | CITTÀ | REPARTO |
|
| MANAGERID |
1001 | VAIBHAVI | MISHRA | 65500 | PUNE | ORACLE |
|
| 1 |
1002 | VAIBHAV | SHARMA | 60000 | NOIDA | C# |
|
| 5 |
1003 | NIKHIL | VANI | 50500 | JAIPUR | FMW |
|
| 2 |
2001 | PRACHI | SHARMA | 55500 | CHANDIGARH | ORACLE |
|
| 1 |
2002 | BHAVESH | JAIN | 65500 | PUNE | FMW |
|
| 2 |
2003 | RUCHIKA | JAIN | 50000 | MUMBAI | C# |
|
| 5 |
3001 | PRANOTI | SHENDE | 55500 | PUNE | JAVA |
|
| 3 |
3002 | ANUJA | WANRE | 50500 | JAIPUR | FMW |
|
| 2 |
3003 | DEEPAM | JAUHARI | 58500 | MUMBAI | JAVA |
|
| 3 |
4001 | RAJESH | GOUD | 60500 | MUMBAI | PROVA |
|
| 4 |
4002 | ASHWINI | BAGHAT | 54500 | NOIDA | JAVA |
|
| 3 |
4003 | RUCHIKA | AGARWAL | 60000 | DELHI | ORACLE |
|
| 1 |
5001 | ARCHIT | SHARMA | 55500 | DELHI | TESTING |
|
| 4 |
Example 1: Write a query that finds employees' minimum salaryfrom the employees' table.
SELECT MIN(SALARY) AS 'MINSALARY' FROM EMPLOYEES;
Wefind the minimum salary from the entire employees' table in the above query.The salary will display as the column name when the output is returned. We havealiased the Min (Salary) expression as min salary.
Output:
MINSALARY |
50000 |
As you can see that the minimum salary is 50000 fromthe employees' table.
Example 2: Write a query to find the minimum salary ofemployees whose city is Pune from the employees' table.
SELECT CITY, MIN(SALARY) AS 'SALARY' FROM EMPLOYEES WHERE CITY = 'PUNE';
We have aliased min (Salary) in the above query,displayed as column name when output is returned. We have found the minimumsalary of an employee whose city is Pune and displayed the city name.
Output:
CITY | SALARY |
PUNE | 55500 |
As you can see, the minimum salary of an employeefrom Pune city is 55500.
Example 3: Write a query to find the employees' minimum salaryfrom the employees' table of employees whose department is Oracle.
SELECT DEPARTMENT, MIN(SALARY) AS 'MINIMUM_SALARY' FROM EMPLOYEES WHERE DEPARTMENT = 'JAVA';
In the above query, we have aliased min (Salary) asminimum_salary, displayed as column name when output is returned. We have foundthe minimum salary of an employee whose department is Java and displayed thedepartment name.
Output:
DEPARTMENT | MINIMUM_SALARY |
JAVA | 54500 |
As you can see, the minimum salary of an employeefrom the Java department is 54500.
Example 4: Write a query to find the minimum salary ofemployees whose department includes one of the lists is Oracle and FMW from theemployees' table.
SELECT MIN(SALARY) AS 'SALARY' FROM EMPLOYEES WHERE DEPARTMENT IN ('ORACLE', 'FMW');
We have aliased min (Salary) in the above query,displayed as column name when output is returned. We have found the minimumsalary of an employee whose department is oracle and FMW.
Output:
SALARY |
50500 |
Example 5: Write a query to find the minimum salary ofemployees whose salary is greater than 50000 and whose city includes Pune,Mumbai from the employees' table group by city.
SELECT CITY, MIN(SALARY) AS EMPLOYEE_SALARY FROM EMPLOYEES WHERE SALARY > 50000 AND CITY IN ('PUNE', 'MUMBAI') GROUP BY CITY;
In the above query, we have aliased Min (Salary) asEMPLOYEE_SALARY, displayed as column name when output is returned. We also usedthe GROUP BY clause followed by the city column. We have found the minimumsalary of an employee whose salary is greater than 50000. Also, the employeecity must be one of the names included in the city IN parameter. The Pune citywill be grouped into one city of all the employees whose city is Pune and foundthe employees' salary, and the same approach is used for Mumbai city.
Output:
CITY | EMPLOYEE_SALARY |
MUMBAI | 58500 |
PUNE | 55500 |
In Mumbai city, the minimum salary is 58500, andPune city's minimum salary is 55500.
Example 6: Write a query to find the minimum employee salaryfrom the employee's table unique cities of employees group by city.
SELECT CITY, MIN(DISTINCT SALARY) AS 'EMPLOYEE_SALARY' FROM EMPLOYEES GROUP BY CITY;
In the above query, we have aliased min (DISTINCT salary)as Employee_salary, displayed as column name when output is returned. We havefound the minimum employee salary from the employees' table considering uniquecities in a group by city column.
Output:
CITY | EMPLOYEE_SALARY |
CHANDIGARH | 55500 |
DELHI | 55500 |
JAIPUR | 50500 |
MUMBAI | 50000 |
NOIDA | 54500 |
PUNE | 55500 |
Example 7: Write aquery to find the minimum employee salary from the employee's table uniquedepartment of employees group by the department.
SELECT DEPARTMENT, MIN(DISTINCT SALARY) AS 'EMPLOYEE_SALARY' FROM EMPLOYEES GROUP BY DEPARTMENT;
In the above query, we have aliased min (DISTINCTsalary) as Employee_salary, displayed as column name when output is returned.We have found the minimum employee salary from the employees' table consideringunique departments grouped by department column.
Output:
DEPARTMENT | EMPLOYEE_SALARY |
C# | 50000 |
FMW | 50500 |
JAVA | 54500 |
ORACLE | 55500 |
TESTING | 55500 |
Example 8: Writea query to find the minimum salary of the employee from the employee's tablegroup by the city where the aggregate salary is greater than 50000
SELECT CITY, MIN(SALARY) AS SALARY FROM EMPLOYEES GROUP BY CITY HAVING MIN(SALARY) > 50000;
We have aliased min (Salary) in the above query,displayed as column name when output is returned. We have found minimum emsployeesalary from the employees' table followed group by city name and used having acondition where aggregate is greater than 50000.
Output:
CITY | SALARY |
CHANDIGARH | 55500 |
DELHI | 55500 |
JAIPUR | 50500 |
NOIDA | 54500 |
PUNE | 55500 |
Here, we can see only cities with aggregate salariesare greater than 50000. Having a clause is like where clause. Having clause isused when we want to apply some condition to the aggregate function. We usedhaving clause only when we used aggregate function in the query.
Example 9: Write a query to find an employee's minimum salarywith a manager group.
SELECT DEPARTMENT, MIN(SALARY) AS SALARY FROM EMPLOYEES WHERE MANAGERID IN (SELECT MANAGERID FROM MANAGER) GROUP BY DEPARTMENT;
We have aliased min (Salary) in the above query,displayed as column name when output is returned. Explanation of the abovequery, First sub-query will get executed (SELECT MANAGERID FROM MANAGER); as aresult, we will get the employee ids, including the null value. After thesub-query is executed, the main query will get executed SELECT DEPARTMENT, MIN(SALARY)AS SALARY FROM EMPLOYEES WHERE MANAGERID IN (output of sub-query). In the INoperator of the main query, there is an employee's id, which is output from thesub-query according to the IN operator. We will get our final result which isthe group by department name.
Output:
DEPARTMENT | SALARY |
C# | 50000 |
FMW | 50500 |
JAVA | 54500 |
ORACLE | 55500 |
TESTING | 55500 |