![]() |
The MySQL MIN() function is used to get the smallest value in a number set. Suppose you have a table with a list of different products and their corresponding prices; you would want to know which one has the lowest price. Here, the MIN() function will return that answer to you in the easiest possible way without searching through all the prices. This will also help in returning the minimum value in any column, be it prices, ages, scores, or any other numerical data. It eases your work and helps you to produce accurate results within the shortest time. MySQL MIN() FunctionThe MySQL MIN() function is used to find the smallest value in a specified column within a table. It’s helpful for quickly identifying the minimum numerical, date, or other data type values without manually searching through all the entries. Syntax of MySQL MIN() FunctionSELECT MIN(column_name) where,
Examples of MySQL MIN() FunctionCreate a TableCREATE TABLE sales ( Insert Data into the TableINSERT INTO sales (product_id, customer_id, amount, sale_date) Output:
Use the MIN() FunctionSELECT MIN(product_id) FROM sales;
Output:
Using MIN() with ConditionsYou can also use the MIN() function along with the WHERE clause to filter the records. For example, if you want to get the minimum amount for sales greater than 150, you can use: Example: Query to Find Minimum Sale Amount Greater Than 150Now we will be understanding the MIN() function with conditions in MySQL with an example SELECT MIN(amount) AS minimum_amount Output:
Explanation: In this example, the query returns the smallest sale amount that is greater than 150. The condition ‘ Using MIN() with GROUP BYThe next example will be using the MIN() function with the GROUP BY clause. We will start by creating a sales table, then insert data into the table which is already stored in the database, and finally run the query returning the minimum amount sold of each product. Example: Query to Find Minimum Sale Amount for Each ProductNow we will be understanding the MIN() GROUP BY function in MySQL with an example: SELECT product_id, MIN(amount) AS minimum_amount Output:
Explanation: This query groups the sales records by ‘ ConclusionThe MySQL MIN() function is an excellent tool for returning users with the smallest value in any column. It can help you find minimum values for numerical, date, or any other data type very quickly and efficiently. Just learn the syntax and different use cases, and you are good to go in efficiently applying the MIN() function into your SQL queries for valuable insights. FAQs of MySQL MIN() FunctionHow to use MIN() in MySQL?
What is MIN () function in SQL?
What do MAX () and MIN () functions do?
|
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 18 |