
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Explain the use of AVG() function in MySQL using Python?
The AVG() function is one of the arithmetic functions in MySQL.
As the name suggests the AVG() function is used to return the average of a numerical column in the table.
Syntax
SELECT AVG(column_name) FROM table_name
Steps you need to follow to use AVG() function on a table using MySQL in python
import MySQLl connector
establish connection with the connector using connect()
create the cursor object using cursor() method
create a query using the appropriate mysql statements
execute the SQL query using execute() method
close the connection
Suppose we have the following table named “Students”.
Students
+----------+-----------+ | name | marks | +----------+-----------+ | Rohit | 62 | | Rahul | 75 | | Inder | 99 | | Khushi | 49 | | Karan | 92 | +----------+-----------+
We want to get the average marks obtained by the students.
Example
import mysql.connector db=mysql.connector.connect(host="your host", user="your username", password="your password",database="database_name") cursor=db.cursor() query1="SELECT AVG(marks) FROM Students" cursor.execute(query1) avg=cursor.fetchall() print(“Average marks :”,avg) db.close()
Output
Average marks : 75.4
- Related Articles
- Can we exclude entries with “0” while using MySQL AVG function?
- Explain the use of SELECT DISTINCT statement in MySQL using Python?
- Explain the use of MIN() and MAX() using MySQL in Python?
- Explain the use of sql LIKE operator using MySQL in Python?
- Explain the use of COUNT() AND SUM() in MySQL using Python?
- Explain and show the use of UNION in MySQL using Python?
- How to use AVG () in Android sqlite?
- What is the fetchone() method? Explain its use in MySQL Python?
- Using MySQL where clause and ordering by avg() to find the average of duplicate individual elements
- What is the use of FIND_IN_SET () function in MySQL?
- What is the use of LOCATE() function in MySQL?
- Explain function of % operator in Python.
- What is the use of MySQL UNHEX() function?
- What is the use of MySQL CHAR() function?
- What is the use of MySQL FROM_UNIXTIME() function?

Advertisements