

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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?
- Can we exclude entries with “0” while using MySQL AVG function?
- How to use AVG () in Android sqlite?
- Explain function of % operator in Python.
- What is the fetchone() method? Explain its use in MySQL Python?
- What is the use of MySQL FROM_UNIXTIME() function?
- What is the use of MySQL GET_FORMAT() function?
- What is the use of MySQL LAST_INSERT_ID() function?
- What is the use of MySQL CONCAT_WS() function?
- What is the use of MySQL TRUNCATE() function?
- What is the use of MySQL UNHEX() function?
Advertisements