EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL MIN()
 

SQL MIN()

Updated March 8, 2023

KoiSQL MIN()

 

 

Introduction to SQL MIN()

MIN() function in standard query language (SQL) is an aggregate function that returns the smallest or minimum value of a specified column obtained in the result set of a SELECT query. It is a very versatile function. It can be used on its own in a SELECT query or used with GROUP BY and HAVING clauses for preparing summary tables in data analytics.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax and Parameters

The basic syntax for writing SQL queries with MIN() function is as follows:

SELECT MIN(column_name)
FROM table_name
WHERE condition;

Parameters used in the above-mentioned syntax:

  • column_name: Field or column for which we want to return the minimum value.
  • table_name: The database table from which the column_name has to be fetched from.
  • WHERE condition: Any filtering clause for rows if required.

The syntax for writing MIN function in GROUPBY clauses:

SELECT MIN(column_name1), column_name2
FROM table_name
GROUP BY column_name2;

Most of the parameters used in this syntax are similar to the former syntax.

  • column_name1: column_name1 corresponds to the column for which the min value has to be returned.
  • column_name2: column_name2 corresponds to the field or column on the basis of which grouping has to be done.

Here we have seen the basic syntax for writing SQL queries with MIN() function. We may use HAVING, ORDER BY etc., based on our requirements.

Examples of SQL MIN()

Given below are the examples of SQL MIN():

In order to illustrate the working of the MIN() function in SQL, let us create a dummy table called “sales”. This table contains details of sales made by different salespeople in a paper company.

The CREATE statement for creating this table is as follows:

Code:

CREATE TABLE sales
(
product_id integer NOT NULL,
sale_date date NOT NULL,
sale_amount numeric NOT NULL,
salesperson character varying(255)  NOT NULL,
store_state character varying(255) NOT NULL,
CONSTRAINT product_key PRIMARY KEY (product_id)
)

Once the table is successfully created, insert the following records in it to work with.

Code:

INSERT INTO sales(
product_id, sale_date, sale_amount, salesperson, store_state)
VALUES (1,'2020-05-06',2300,'X','DL'),
(2,'2020-05-06',5300,'Y','DL'),
(3,'2020-05-06',300,'X','MH'),
(4,'2020-05-07',4200,'Y','MH'),
(5,'2020-05-07',900,'Y','MH'),
(6,'2020-05-05',600,'X','DL'),
(7,'2020-05-05',1450,'Y','MH'),
(8,'2020-05-05',987,'X','MH');
select * from sales;

Output:

SQL MIN()1

The query returned successfully. Now we are all set to try a few examples based on MIN()function using the sales table.

Example #1 – Basic function of MIN()

Find the minimum amount of sale made overall.

Code:

SELECT MIN(sale_amount)
FROM sales;

Output:

SQL MIN()2

In this example, MIN() has been used on the sale_amount column to find the minimum value in this column.

Example #2

Find the earliest date on which the first sale was made (as per the sales table).

Code:

SELECT MIN(sale_date)
FROM sales;

Output:

SQL MIN()3

Example #3 – MIN() with WHERE CLAUSE.

Find the minimum amount of sale made for the Delhi or DL store location.

Code:

SELECT MIN(sale_amount) as "Minimum sale"
FROM sales
WHERE store_state = 'DL';

Output:

SQL MIN()4

Example #4 – MIN() with GROUP BY CLAUSE.

Find the minimum amount of sale made overall grouped together by store location.

Code:

SELECT store_state, MIN(sale_amount) as "Minimum sale"
FROM sales
GROUP BY store_state;

Output:

SQL MIN()5

Here, the SELECT statement results are grouped together by store_state, and then the corresponding minimum value for each group has been fetched using MIN() function.

Example #5

Find the minimum amount of sale made overall grouped together by the salesperson.

Code:

SELECT salesperson, MIN(sale_amount) as "Minimum sale"
FROM sales
GROUP BY salesperson;

Output:

overall grouped together by the salesperson

Example #6

Find the minimum amount of sale made overall grouped together by the date of sale and salesperson.

Code:

SELECT sale_date as "Date of sale",
salesperson, MIN(sale_amount) as "Minimum sale"
FROM sales
GROUP BY sale_date, salesperson;

Output:

SQL MIN()7

In this example, we have grouped the result set by two fields, namely sale_date and salesperson and then summarized each group using MIN().

Example #7 – MIN() with HAVING CLAUSE.

Find the total sale amount grouped together by the salesperson having the earliest date of the first sale as 5th May 2020.

Code:

SELECT salesperson, SUM(sale_amount) as "Total sales"
FROM sales
GROUP BY salesperson
HAVING MIN(sale_date) = '2020-05-05';

Output:

total amount of sale made overall grouped together

Here, HAVING MIN(sale_date) acts as a filtering clause for this aggregate query. 

Example #8 – MIN() with ORDER BY CLAUSE.

Find the minimum amount of sale made overall grouped together by the date of sale and salesperson, ordered by minimum sale amount.

Code:

SELECT sale_date as "Date of sale",
salesperson, MIN(sale_amount) as "Minimum sale"
FROM sales
GROUP BY sale_date, salesperson
ORDER BY MIN(sale_amount);

Output:

sale made overall grouped together

Advantages of SQL MIN()

Given below are the advantages mentioned:

  • MIN() is a statistical function that helps to find the minimum value of a specific field in the result set of a SELECT query.
  • MIN(), when used as a summary function in the GROUP BY clause, it helps in finding the Minimum value of records in each group.
  • MIN() when used with a HAVING clause, it helps to filter records based on the minimum requisite value.

Conclusion

MIN() function is an aggregate function that is used to find the minimum value of a selected column.

Recommended Articles

We hope that this EDUCBA information on “SQL MIN()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. SQL ORDER BY Ascending
  2. SQL ORDER BY Alphabetical
  3. SQL NOT Operator
  4. SQL Table Partitioning

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW