×

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression.

The syntax for the select min function:

SELECT MIN(column_name)
FROM table_name WHERE conditions;

Let’s take deep dive into the SQL SELECT MIN.

Consider the existing tables, which have the following records:

Table: Employees

EMPLOYEEID

FIRST_NAME

LAST_NAME

SALARY

CITY

DEPARTMENT

 

 

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

TESTING

 

 

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 salary from the employees' table.

SELECT MIN(SALARY) AS 'MINSALARY' FROM
EMPLOYEES;

We find 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 have aliased the Min (Salary) expression as min salary.

Output:

MINSALARY

50000


SQL SELECT MIN

As you can see that the minimum salary is 50000 from the employees' table.

Example 2: Write a query to find the minimum salary of employees 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 minimum salary of an employee whose city is Pune and displayed the city name.

Output:

CITY

SALARY

PUNE

55500


SQL SELECT MIN

As you can see, the minimum salary of an employee from Pune city is 55500.

Example 3: Write a query to find the employees' minimum salary from 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) as minimum_salary, displayed as column name when output is returned. We have found the minimum salary of an employee whose department is Java and displayed the department name.

Output:

DEPARTMENT

MINIMUM_SALARY

JAVA

54500


SQL SELECT MIN

As you can see, the minimum salary of an employee from the Java department is 54500.

Example 4: Write a query to find the minimum salary of employees whose department includes one of the lists is Oracle and FMW from the employees' 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 minimum salary of an employee whose department is oracle and FMW.

Output:

SALARY

50500


SQL SELECT MIN

Example 5: Write a query to find the minimum salary of employees 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) as EMPLOYEE_SALARY, displayed as column name when output is returned. We also used the GROUP BY clause followed by the city column. We have found the minimum salary of an employee whose salary is greater than 50000. Also, the employee city must be one of the names included in the city IN parameter. The Pune city will be grouped into one city of all the employees whose city is Pune and found the employees' salary, and the same approach is used for Mumbai city.

Output:

CITY

EMPLOYEE_SALARY

MUMBAI

58500

PUNE

55500


SQL SELECT MIN

In Mumbai city, the minimum salary is 58500, and Pune city's minimum salary is 55500.

Example 6: Write a query to find the minimum employee salary from 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 have found the minimum employee salary from the employees' table considering unique cities in a group by city column.

Output:

CITY

EMPLOYEE_SALARY

CHANDIGARH

55500

DELHI

55500

JAIPUR

50500

MUMBAI

50000

NOIDA

54500

PUNE

55500


SQL SELECT MIN

Example 7:  Write a query to find the minimum employee salary from the employee's table unique department 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 (DISTINCT salary) as Employee_salary, displayed as column name when output is returned. We have found the minimum employee salary from the employees' table considering unique departments grouped by department column.

Output:

DEPARTMENT

EMPLOYEE_SALARY

C#

50000

FMW

50500

JAVA

54500

ORACLE

55500

TESTING

55500


SQL SELECT MIN

Example 8: Write a query to find the minimum salary of the employee from the employee's table group 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 emsployee salary from the employees' table followed group by city name and used having a condition where aggregate is greater than 50000.

Output:

CITY

SALARY

CHANDIGARH

55500

DELHI

55500

JAIPUR

50500

NOIDA

54500

PUNE

55500


SQL SELECT MIN

Here, we can see only cities with aggregate salaries are greater than 50000. Having a clause is like where clause. Having clause is used when we want to apply some condition to the aggregate function. We used having clause only when we used aggregate function in the query.

Example 9: Write a query to find an employee's minimum salary with 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 above query, First sub-query will get executed (SELECT MANAGERID FROM MANAGER); as a result, we will get the employee ids, including the null value. After the sub-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 IN operator of the main query, there is an employee's id, which is output from the sub-query according to the IN operator. We will get our final result which is the group by department name.

Output:

DEPARTMENT

SALARY

C#

50000

FMW

50500

JAVA

54500

ORACLE

55500

TESTING

55500


SQL SELECT MIN


Related Topics

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.