SQL MIN() and MAX() are functions that are used to find the minimum and maximum values, respectively, in a column of a table in a SQL database.
These functions are commonly used in queries to retrieve specific data from a database.
Using MIN()
The MIN() function is used to find the smallest value in a column.
The basic syntax for using the MIN() function is as follows:
SELECT MIN(column_name)
FROM table_name;
For example, if you have a table named "employees" with a column named "salary", you can find the lowest salary in the table using the following query:
SELECT MIN(salary)
FROM employees;
You can also use the MIN() function in conjunction with other clauses in a query.
For example, you can use it in combination with a WHERE clause to find the lowest salary of employees in a specific department:
SELECT MIN(salary)
FROM employees
WHERE department = 'IT';
Using MAX()
The MAX() function is used to find the largest value in a column.
The basic syntax for using the MAX() function is similar to that of the MIN() function:
SELECT MAX(column_name)
FROM table_name;
For example, if you have a table named "employees" with a column named "salary", you can find the highest salary in the table using the following query:
SELECT MAX(salary)
FROM employees;
You can also use the MAX() function in conjunction with other clauses in a query.
For example, you can use it in combination with a WHERE clause to find the highest salary of employees in a specific department:
SELECT MAX(salary)
FROM employees
WHERE department = 'IT';
Combining MIN() and MAX()
It is also possible to use both the MIN() and MAX() functions in a single query to find both the smallest and largest values in a column.
For example, the following query finds both the lowest and highest salaries in the "employees" table:
SELECT MIN(salary), MAX(salary)
FROM employees;
You can also use it in conjunction with GROUP BY clause to find the min and max values grouped by a specific column.
SELECT department, MIN(salary), MAX(salary)
FROM employees
GROUP BY department;
In conclusion, the MIN() and MAX() functions in SQL are useful for finding the smallest and largest values in a column, respectively.
These functions can be used on their own or in combination with other clauses in a query to retrieve specific data from a database.