SQL Server MIN()



SQL Server MIN() Function

The SQL Server MIN() function returns the minimum value in a set of values. This function is useful for finding the lowest value in a column.


Syntax

SELECT MIN(column_name)
FROM table_name;

The MIN() function takes a single argument:

  • column_name: The name of the column for which to find the minimum value.

Example SQL Server MIN() Function Queries

Let's look at some examples of SQL Server MIN() function queries:

1. Basic MIN() Example

SELECT MIN(salary) AS lowest_salary
FROM employees;

This query returns the lowest value in the salary column of the employees table. The result will be:

lowest_salary
--------------
45000

2. MIN() with a Condition

SELECT MIN(salary) AS lowest_salary
FROM employees
WHERE department = 'Sales';

This query returns the lowest value in the salary column for employees in the 'Sales' department. The result will be:

lowest_salary
--------------
50000

3. MIN() with a Group By Clause

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

This query returns the lowest value in the salary column for each department. The result will show the department and the corresponding lowest_salary.

4. MIN() with a Variable

DECLARE @dept VARCHAR(50);
SET @dept = 'Engineering';
SELECT MIN(salary) AS lowest_salary
FROM employees
WHERE department = @dept;

This query uses a variable to specify the department and then returns the lowest salary for that department. The result will be:

lowest_salary
--------------
60000

Full Example

Let's go through a complete example that includes creating a table, inserting data, and using the MIN() function.

Step 1: Creating a Table

This step involves creating a new table named employees to store some sample data.

CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    department VARCHAR(50),
    salary DECIMAL(10, 2)
);

In this example, we create a table named employees with columns for id, name, department, and salary.

Step 2: Inserting Data into the Table

This step involves inserting some sample data into the employees table.

INSERT INTO employees (id, name, department, salary) VALUES (1, 'John Doe', 'Engineering', 75000);
INSERT INTO employees (id, name, department, salary) VALUES (2, 'Jane Smith', 'Sales', 50000);
INSERT INTO employees (id, name, department, salary) VALUES (3, 'Jim Brown', 'Engineering', 60000);
INSERT INTO employees (id, name, department, salary) VALUES (4, 'Jake White', 'Sales', 70000);
INSERT INTO employees (id, name, department, salary) VALUES (5, 'Jill Green', 'HR', 45000);

Here, we insert data into the employees table.

Step 3: Using the MIN() Function

This step involves using the MIN() function to return the lowest value in the salary column.

SELECT MIN(salary) AS lowest_salary
FROM employees;

This query retrieves the lowest value in the salary column for all rows in the employees table. The result will be:

lowest_salary
--------------
45000

Conclusion

The SQL Server MIN() function is a powerful tool for returning the minimum value in a set of values. Understanding how to use the MIN() function and its syntax is essential for effective data analysis and processing in SQL Server.