Articles

Python MySQL MIN Aggregate function

Python MySQL MIN Aggregate function


In this python tutorial, we will see how to perform MIN aggregate function in mysql database in xampp through python.

SQL MIN

 

In sql, MIN() function returns the minimum value of the columns having numeric value.

Syntax for MIN()

SELECT MIN(column_name)
FROM table_name
WHERE condition;

Now, let' see how to return minimum values in a column using python

Before that make sure that you have created a database named - tutorialsinhand  and table inside it  market with the following records.

 

Steps:

1. Import mysql.connector module
    Syntax:
    import mysql.connector

2. Create the connection object to connect with xampp server using the below credentials.
    Syntax:
    connection_obj = mysql.connector.connect(
                          host="localhost",
                          user="root",
                          password=""
                                                                        )
3. Create cursor object to execute the query.
    Syntax:
    connection_obj.cursor()

4. Write sql query to select columns by applying MIN aggregate function on a column.
   
5. Fetch the resulted rows using fetchall().
    Syntax:
    cursor.fetchall()

6. Display using for loop 
     Syntax:
         for i in results:
            print(i)

Note - It will return the values in a tuple.

Example:

In this example, we will return minimum value in ratings column.

#import the module
import mysql.connector

#create the connection by specifying the 
#hostname-localhost
#username-root
#password-''(empty) and
#database is tutorialsinhand
connection_obj = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="tutorialsinhand"
)

#creating cursor object
cursor_obj = connection_obj.cursor()


#return minimum from rating column
cursor_obj.execute("SELECT MIN(rating) FROM market")

#fetch the rows
results = cursor_obj.fetchall()

#get the values
for i in results:
  print("Minimum rating: ",i[0])

Output:

Minimum rating:  2

We can see that minimum rating is 2.

 

Let's see how to use MIN Aggregate function with GROUP BY.

SQL GROUP BY

 

In Sql, GROUP BY statement is mostly used along with the aggregate functions to group the result based on one or multiple columns.

MIN() with GROUP BY will return minimum values in a group.

Syntax of GROUP BY

SELECT MIN(column_name)
FROM table_name
GROUP BY column_name;

Important points

  • We can specify multiple columns in GROUP BY.
  • We can use more than one aggregate function for grouping the result-set.
  • If ORDER BY is used then it should only be used after GROUP BY.

Example: In this example, we are grouping rating with market_area column, so it will group the values in market_area column and return minimum value  in each group.

#import the module
import mysql.connector

#create the connection by specifying the 
#hostname-localhost
#username-root
#password-''(empty) and
#database is tutorialsinhand
connection_obj = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="tutorialsinhand"
)

#creating cursor object
cursor_obj = connection_obj.cursor()


#return minimum from rating column by grouping market_area column
cursor_obj.execute("SELECT market_area,MIN(rating) FROM market GROUP BY market_area")

#fetch the rows
results = cursor_obj.fetchall()

#get the values
for i in results:
  print("Market Area: ",i[0]," & ", "Minimum-Rating: ",i[1])
  print()

 

Output:

We can see that 4 groups are returned by grouping similar values in market_area column and returned minimum  rating  in each groups.

Market Area:  bengal  &  Minimum-Rating:  2

Market Area:  guntur  &  Minimum-Rating:  8

Market Area:  hyderabad  &  Minimum-Rating:  3

Market Area:  kakumanu  &  Minimum-Rating:  4

 


Python MySql

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Nov 06,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!