Basic about MIN Function – using and example

This SQL tutorial explains how to use the SQL MIN characteristic with syntax and examples.

Description

The SQL MIN function is used to return the minimal value of an expression in a SELECT statement.

Syntax

The syntax for the MIN function in SQL is:

SELECT MIN(aggregate_expression)
FROM tables
[WHERE conditions];

OR the syntax for the MIN function when grouping the outcomes by way of one or more columns is:

SELECT expression1, expression2, ... expression_n,
       MIN(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;

Parameters or Arguments

expression1, expression2, … expression_n Expressions that are now not encapsulated inside the MIN characteristic and need to be blanketed in the GROUP BY clause at the cease of the SQL statement. aggregate_expression This is the column or expression from which the minimal cost will be returned. tables The tables that you wish to retrieve archives from. There have to be at least one desk listed in the FROM clause. WHERE conditions Optional. These are prerequisites that must be met for the data to be selected.

Example – With Single Expression

The easiest way to use the SQL MIN function would be to return a single area that calculates the MIN value.

For example, you may desire to recognize the minimal salary of all employees.

SELECT MIN(salary) AS "Lowest salary"
FROM employees;

In this SQL MIN feature example, we’ve got aliased the MIN(salary) field as “Lowest salary”. As a result, “Lowest salary” will display as the subject title when the result set is returned.

Example – Using SQL GROUP BY

In some cases, you will be required to use the SQL GROUP BY clause with the SQL MIN function.

For example, you may want to additionally use the SQL MIN function to return the identify of each department and the minimal profits in the department.

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

Because you have listed one column in your SQL SELECT announcement that is not encapsulated in the SQL MIN function, you should use the SQL GROUP BY clause. The branch field must, therefore, be listed in the GROUP BY section.