Open In App

MariaDB MIN Functions

Last Updated : 22 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MariaDB is a relational database language that is similar to SQL. We know that in a relational database language, the data is stored in the form of relations which are nothing but the tables. Similar to SQL which has aggregate functions such as MIN(), MAX(), AVG(), and LEAST() functions. These aggregate functions in the MariaDB help us to make operations faster and easier. MariaDB is a cost-effective solution for small and large web projects compared to other databases.

In this article, We will learn about the MIN Function in MariaDB along with its syntax, practical examples of some functions, and so on.

MIN Function in MariaDB

MIN Function in MariaDB is a function that is used to return the smallest or minimum value of columns. With the help of the MIN, the Function user can easily get the minimum value of the table or database. Let’s understand some examples for better understanding.

Note: We can perform the MIN, and MAX operations with the numerical data as well as the Strings.

Examples of MariaDB MIN Functions

For a better understanding of Min Function in MariaDB, we need a table on which we will perform various operations and queries. So in this article, we have a DETAILS table which consists of id, NAME, and SUBJECT as Columns. If you don’t know How to Create a Table in SQLite then refer to this. After inserting data into the DETAILS Table. The table looks:

Output:

Details

Deails Table

Explanation: In the above image we have retreived all the records from the table DETAILS.

Example 1: Using the MIN( ) Function to Get Minimum Value

In this example, We are going to use the MIN() function with the table DETAILS. Generally as mentioned earlier the aggregate operators are used to make the operation easier.

Generally the MIN() function is used to return the minimum value from the given column in the relation. When we use the MIN() function it checks for the lexographical order and returns the string based on that.

Query:

SELECT MIN(NAME) FROM DETAILS;

Output:

GetMin

Output

Explanation:

  • In the above image we have used the MIN() function along with the string fields.
  • The MIN() Function when used along with the Min .It returns the smallest string in the lexographical order.
  • As Durga is smallest in the lexographic order across all name as it returns Durga.

Example 2 :Using MIN() Function With the Numeric Fields

Let’s find the minimum id in the DETAILS Table.

Query:

SELECT MIN(id) FROM DETAILS;

Output:

NumericField

Output

Explanation:

  • In the above image we have used the MIN() function along with the numeric fields.
  • Here id is having the numerical values.
  • So when we use the MIN() function along with the id. It returns 1 as 1 is the smallest value.

Example 3: Using the MIN() Function with GROUPBY Clause

In this query we are going to use GROUP BY Clause . The GROUP BY Clause is used to group a particular field with the help of aggregate functions. In this query we will use the aggregate functions such as LENGTH and COUNT .

  • COUNT : The COUNT() function is used return the number of fields that have particular value in the relation.
  • LENGTH : The LENGTH() is used to return the length of a string type .The Length is mainly used with the string fields .

In the below query we are going to extract the Minimum name in number of names grouped by their length. Generally when they are grouped by the length they give the name on the basis of the lexographic order.

Query:

SELECT MIN(name) , LENGTH(NAME) AS NAME_LENGTH FROM students GROUP BY LENGTH(NAME) ;

Output:

MIN-WITH-GROUP-BY-RECORDS-

MIN WITH GROUP BY OUTPUT

Explanation: In the above Query, We have fetched the name of person based on the length of name (string). The output names are sorted according to their names length.

Example 4: Using the MIN Function Along With the HAVING Clause

In this query we are going to use HAVING Clause along with the MIN function. Generally we use the HAVING Clause along with the aggregate operators. In the below query we are going to extract the name which is of lowest lexographical order from the records whose length is of exactly 5. We have grouped the records with the name.

Query:

SELECT MIN(name) , LENGTH(name) FROM students GROUP BY name HAVING LENGTH(name)=5;

Output:

MIN-HAVING-OUTPUT

MIN HAVING OUTPUT

Explanation: In the above Query, We have fetched name of person and the length of name string. Here we have grouped the name of DETAILS and find the name along with length of name(string) is 5.

CONCLUSION

Aftere reading the whole article now we have good understanding of MIN Function in MariaDB. MariaDB is used to find the minimum/smallest value according to the conditions of queries. Finally aggregate operators are used to derive the descriptive statistics. The aggregations are used to compute the mathematical calculations. For example the data of population and censux are very large.The manual caluculation for this takes a large ammount of time . In order to make the caluculations easier we use the aggregate operations.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads