
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - MIN() - MAX() function
The MIN() and MAX() functions in SQL are aggregate functions. They are used to compare values in a set and, retrieve the maximum and minimum values respectively.
An aggregate function is a mathematical computation that takes a range of values as input and yields a single value expression, representing the significance of the provided data.
MAX() and MIN() aggregate functions are generally used in two ways:
As functions, they are used with the GROUP BY clause of the SELECT statement.
As expressions, they are used with a subquery and HAVING clause of SELECT statement.
The SQL MAX() Function
The MAX() function compares the values in a column and returns the largest value among them.
Syntax
Following is the syntax of SQL MAX() function −
MAX(column_name);
Example
In the following example, we are running a query for MAX() function on a table named CUSTOMERS. The objective is to retrieve the maximum salary value from this table. First of all, let us create the CUSTOMERS table using the following query −
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00), (2, 'Khilan', 25, 'Delhi', 1500.00), (3, 'Kaushik', 23, 'Kota', 2000.00), (4, 'Chaitali', 25, 'Mumbai', 6500.00), (5, 'Hardik', 27, 'Bhopal', 8500.00), (6, 'Komal', 22, 'Hyderabad', 4500.00), (7, 'Muffy', 24, 'Indore', 10000.00);
The CUSTOMERS table will be created as −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Here, we are comparing the salaries of CUSTOMERS and retrieving the maximum salary using the following query −
SELECT MAX(SALARY) FROM CUSTOMERS;
When the above query is executed, the result is displayed as −
MAX(SALARY) |
---|
10000.0000 |
HAVING with MAX() Function
In the following query, we are fetching the ID, NAME, and SALARY of the CUSTOMERS using the MAX() function along with HAVING clause.
SELECT ID, NAME, SALARY FROM CUSTOMERS GROUP BY NAME, ID HAVING MAX(SALARY) < 8000;
When the above query is executed, we get the details of the employees whose maximum salary is less than 8000 −
ID | NAME | SALARY |
---|---|---|
1 | Ramesh | 2000.00 |
2 | Khilan | 1500.00 |
3 | Kaushik | 2000.00 |
4 | Chaitali | 6500.00 |
6 | Komal | 4500.00 |
MAX() Function in Subqueries
In the following example, we are using the MAX() function in a subquery to retrieve the record with maximum salary, from the CUSTOMERS table.
SELECT * FROM CUSTOMERS WHERE SALARY = (SELECT MAX(SALARY) FROM CUSTOMERS);
When we execute the above query, we will get the following result −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
7 | Muffy | 24 | Indore | 10000.00 |
MAX() Function with Strings
This query retrieves the maximum value (alphabetically) among the names of customers in the CUSTOMERS table using the MAX() function −
SELECT MAX(NAME) AS max_name FROM CUSTOMERS;
Following is the result of the above query −
max_name |
---|
Ramesh |
Aliases with MAX() Function
In the following example, we use the MAX() function to retrieve the record containing maximum age from the CUSTOMERS table. We are displaying the results as a new column with the alias "max_age".
SELECT MAX(age) AS 'max_age' FROM CUSTOMERS;
Following is the output of the above query −
max_age |
---|
32 |
The SQL MIN() Function
The MIN() function compares values in a column and returns the smallest value among them.
Syntax
Following is the syntax of SQL MIN() function −
MIN(column_name);
Example
In this example, we are comparing values in the SALARY column of CUSTOMERS table and displaying the minimum salary using the following query −
SELECT MIN(SALARY) FROM CUSTOMERS;
When the above query is executed, the result is displayed as −
MIN(SALARY) |
---|
1500.0000 |
HAVING with MIN() Function
In the following query, we are fetching the ID, NAME, and SALARY of the CUSTOMERS using the MIN() function along with HAVING clause.
SELECT ID, NAME, SALARY FROM CUSTOMERS GROUP BY NAME, ID HAVING MIN(SALARY) > 5000;
When the above query is executed, we get the details of the maximum salary for employees whose minimum salary is more than 5000, as we can see in the table that follows −
ID | NAME | MAX_Salary |
---|---|---|
4 | Chaitali | 6500.0000 |
5 | Hardik | 8500.0000 |
7 | Muffy | 10000.0000 |
MIN() Function in Subqueries
In the following example, we are using the MIN() function in a subquery to retrieve the record with minimum salary, from the CUSTOMERS table.
SELECT * FROM CUSTOMERS WHERE SALARY = (SELECT MIN(SALARY) FROM CUSTOMERS);
When we execute the above query, we will get the following result −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
2 | Khilan | 25 | Delhi | 1500.00 |
MIN() Function with Strings
Following is the query to retrieve the minimum value (alphabetically) among the names of customers in the CUSTOMERS table using the MIN() function −
SELECT MIN(NAME) AS min_first_name FROM CUSTOMERS;
Following is the result of the above query −
min_first_name |
---|
Chaitali |
Aliases with MIN() Function
Following is the SQL query that will fetch the minimum age from the CUSTOMERS table using the MIN() function −
SELECT MIN(age) AS 'min_age' FROM CUSTOMERS;
When we execute the above query, the minimum value in the age field is displayed as shown below.
min_age |
---|
22 |