SQL AGGREGATE FUNCTIONS – MIN

SQL AGGREGATE FUNCTIONS – MIN

SQL has 5 different aggregate functions (MIN, MAX, COUNT, SUM & AVG). Here we will be looking at the MIN function. The MIN function returns the minimum value of a column in the table. 

SYNTAX – 

SELECT MIN(column1) FROM table_name WHERE condition;

Usually, the aggregate functions are used in conjunction with the GROUP BY/ HAVING clause, which gives even more meaningful results. The syntax for the query using MIN function using GROUP BY / HAVING clause is mentioned below –

SELECT column1, MIN(column2) FROM table_name WHERE condition1 GROUP BY column1 HAVING condition2;

At first, these might look little complicated, but once we go over few examples, it will make more sense. 



Illustration – Consider the below employee table in the database which stores the information pertaining to employees in a company –

 

IDemployee_nameagesalaryDepartmentmanager_name
1Jimmy3280000SalesVijay
2James3585000SalesVijay
3Helen3382000HRKrish
4Mary2678000QualityWarner
5William3390000HRKrish
6Vijay38105000SalesRoger
7Krish38105000HRRoger

Query 1 – What is the minimum salary earned by any employee in ABC company?

  SELECT MIN(salary) FROM employee;

MIN(salary)

————— 

78000

Query 2 – What is the minimum salary earned by any employee in ABC company whose age is greater than 30 years?

  SELECT MIN(salary) FROM employee WHERE age>30;

MIN(salary)

————— 

80000

Query 3 – What is the minimum salary earned by employees in ABC company by each department.

  SELECT Department, MIN(salary) FROM employee GROUP BY Department;

DepartmentMIN(Salary)
Sales80000
HR82000
Quality78000

 

Query 4 – What is the minimum salary earned by employees in ABC company by each department that gets at least 80000 as salary.

  SELECT Department, MIN(salary) FROM employee GROUP BY Department HAVING MIN(salary) >= 80000;

DepartmentMIN(Salary)
Sales80000
Quality78000

 

 

 

Leave A Reply

Your email address will not be published. Required fields are marked *