Class 12 Programs
- 1.Read text file line by line with # separator
- 2.Count vowels, consonants, uppercase, lowercase in file
- 3.Remove lines containing character 'a'
- 4.Binary file with name and roll number search
- 5.Binary file update marks by roll number
- 6.Random number generator (Dice Simulator)
- 7.Stack implementation using list
- 8.CSV file with user-id and password
- 9.User Defined Functions to manipulate List Store Indices of Non Zero elements &Double the Odd values
- 10.WORKING WITH BINARY FILE IN PYTHON Create a binary file, Search and display the records from the binary file
- 11.TEXT FILES IN PYTHON Remove duplicate lines from the file & Display unique words present in the file
- 12.TEXT FILES IN PYTHON Copying lines to a new file & Replacing the '-' sign with a blankspace
- 13.TEXT FILES IN PYTHON Count the lines starts with I/T & Display the lines with exactly 6 words
- 14.TEXT FILES IN PYTHON Count the occurrences of word & Count number of vowels and consonants
- 15.User Defined Functions to Manipulate List Find the Longest Word and Shifting the elements to left
- 16.IMPLEMENTATION OF STACK USING LIST IN PYTHON
- 17.ALTER table to add new attributes / modify data type / drop attribute
- 18.UPDATE table to modify data
- 19.ORDER By to display data in ascending / descending order
- 20.DELETE to remove tuple(s)
- 21.GROUP BY and find the min, max, sum, count and average
- 22.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - DELETE RECORDS
- 23.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - UPDATE RECORDS
- 24.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - INSERT RECORDS
Quick Tips
- • Import random module for number generation
- • Use randint(1, 6) for dice simulation
- • Handle user input validation properly
INTERFACING PYTHON WITH MYSQL - GROUP BY OPERATIONS
AIM
To write a Python program to interface with MySQL database and demonstrate GROUP BY operations with aggregate functions like COUNT, SUM, AVG, MAX, and MIN.
ALGORITHM
- Start
- Import mysql.connector module
- Establish connection to MySQL database
- Create cursor object
- Create sample data if not exists
- Display original data
- Execute GROUP BY queries with different aggregate functions
- Display results for each GROUP BY operation
- Demonstrate HAVING clause with GROUP BY
- Close cursor and connection
- Stop
PROGRAM
# Python program to demonstrate GROUP BY operations with MySQL
import mysql.connector
def connect_database():
"""Establish connection to MySQL database"""
try:
connection = mysql.connector.connect(
host='localhost',
database='sales_db',
user='root',
password='password'
)
return connection
except mysql.connector.Error as error:
print(f"Error connecting to MySQL: {error}")
return None
def create_sample_data():
"""Create sample sales data"""
connection = connect_database()
if connection is None:
return
cursor = connection.cursor()
try:
# Create sales table
cursor.execute("""
CREATE TABLE IF NOT EXISTS sales (
sale_id INT PRIMARY KEY,
product VARCHAR(50),
category VARCHAR(30),
quantity INT,
price DECIMAL(10,2),
sale_date DATE
)
""")
# Insert sample data
sample_data = [
(1, 'Laptop', 'Electronics', 2, 50000.00, '2024-01-15'),
(2, 'Mouse', 'Electronics', 5, 1500.00, '2024-01-16'),
(3, 'Book', 'Education', 10, 500.00, '2024-01-17'),
(4, 'Pen', 'Education', 20, 50.00, '2024-01-18'),
(5, 'Phone', 'Electronics', 3, 30000.00, '2024-01-19'),
(6, 'Notebook', 'Education', 15, 100.00, '2024-01-20'),
(7, 'Tablet', 'Electronics', 1, 25000.00, '2024-01-21'),
(8, 'Pencil', 'Education', 25, 20.00, '2024-01-22')
]
cursor.executemany(
"INSERT IGNORE INTO sales VALUES (%s, %s, %s, %s, %s, %s)",
sample_data
)
connection.commit()
print("Sample sales data created!")
except mysql.connector.Error as error:
print(f"Error creating sample data: {error}")
finally:
cursor.close()
connection.close()
def display_all_data():
"""Display all sales data"""
connection = connect_database()
if connection is None:
return
cursor = connection.cursor()
try:
cursor.execute("SELECT * FROM sales")
records = cursor.fetchall()
print("\n--- All Sales Data ---")
print("ID\tProduct\t\tCategory\tQty\tPrice\t\tDate")
print("-" * 70)
for record in records:
print(f"{record[0]}\t{record[1]:<12}\t{record[2]:<12}\t{record[3]}\t{record[4]}\t{record[5]}")
except mysql.connector.Error as error:
print(f"Error displaying data: {error}")
finally:
cursor.close()
connection.close()
def group_by_operations():
"""Demonstrate various GROUP BY operations"""
connection = connect_database()
if connection is None:
return
cursor = connection.cursor()
try:
# 1. Count products by category
print("\n1. COUNT of products by category:")
cursor.execute("SELECT category, COUNT(*) as product_count FROM sales GROUP BY category")
results = cursor.fetchall()
print("Category\t\tCount")
print("-" * 25)
for row in results:
print(f"{row[0]:<15}\t{row[1]}")
# 2. Total quantity sold by category
print("\n2. SUM of quantity by category:")
cursor.execute("SELECT category, SUM(quantity) as total_quantity FROM sales GROUP BY category")
results = cursor.fetchall()
print("Category\t\tTotal Qty")
print("-" * 25)
for row in results:
print(f"{row[0]:<15}\t{row[1]}")
# 3. Average price by category
print("\n3. AVERAGE price by category:")
cursor.execute("SELECT category, AVG(price) as avg_price FROM sales GROUP BY category")
results = cursor.fetchall()
print("Category\t\tAvg Price")
print("-" * 30)
for row in results:
print(f"{row[0]:<15}\t{row[1]:.2f}")
# 4. Maximum and minimum price by category
print("\n4. MAX and MIN price by category:")
cursor.execute("SELECT category, MAX(price) as max_price, MIN(price) as min_price FROM sales GROUP BY category")
results = cursor.fetchall()
print("Category\t\tMax Price\tMin Price")
print("-" * 40)
for row in results:
print(f"{row[0]:<15}\t{row[1]}\t\t{row[2]}")
# 5. Total sales value by category
print("\n5. Total sales value by category:")
cursor.execute("SELECT category, SUM(quantity * price) as total_sales FROM sales GROUP BY category")
results = cursor.fetchall()
print("Category\t\tTotal Sales")
print("-" * 30)
for row in results:
print(f"{row[0]:<15}\t{row[1]:.2f}")
# 6. GROUP BY with HAVING clause
print("\n6. Categories with total sales > 50000:")
cursor.execute("""
SELECT category, SUM(quantity * price) as total_sales
FROM sales
GROUP BY category
HAVING SUM(quantity * price) > 50000
""")
results = cursor.fetchall()
print("Category\t\tTotal Sales")
print("-" * 30)
for row in results:
print(f"{row[0]:<15}\t{row[1]:.2f}")
# 7. Multiple column GROUP BY
print("\n7. Sales summary by category and date:")
cursor.execute("""
SELECT category, sale_date, COUNT(*) as items, SUM(quantity * price) as daily_sales
FROM sales
GROUP BY category, sale_date
ORDER BY category, sale_date
""")
results = cursor.fetchall()
print("Category\t\tDate\t\tItems\tDaily Sales")
print("-" * 50)
for row in results:
print(f"{row[0]:<15}\t{row[1]}\t{row[2]}\t{row[3]:.2f}")
except mysql.connector.Error as error:
print(f"Error in GROUP BY operations: {error}")
finally:
cursor.close()
connection.close()
// Main program
if __name__ == "__main__":
print("=== MySQL Database - GROUP BY Operations ===")
// Create sample data
create_sample_data()
// Display all data
display_all_data()
// Demonstrate GROUP BY operations
group_by_operations()
print("\n=== GROUP BY Operations completed ===")
}
OUTPUT
=== MySQL Database - GROUP BY Operations ===
Sample sales data created!
--- All Sales Data ---
ID Product Category Qty Price Date
----------------------------------------------------------------------
1 Laptop Electronics 2 50000.0 2024-01-15
2 Mouse Electronics 5 1500.0 2024-01-16
3 Book Education 10 500.0 2024-01-17
4 Pen Education 20 50.0 2024-01-18
1. COUNT of products by category:
Category Count
-------------------------
Electronics 4
Education 4
2. SUM of quantity by category:
Category Total Qty
-------------------------
Electronics 11
Education 70
3. AVERAGE price by category:
Category Avg Price
------------------------------
Electronics 26625.00
Education 167.50
4. MAX and MIN price by category:
Category Max Price Min Price
----------------------------------------
Electronics 50000.0 1500.0
Education 500.0 20.0
5. Total sales value by category:
Category Total Sales
------------------------------
Electronics 132500.00
Education 7500.00
6. Categories with total sales > 50000:
Category Total Sales
------------------------------
Electronics 132500.00
=== GROUP BY Operations completed ===
CONCLUSION
The program successfully demonstrates various GROUP BY operations in MySQL using Python. It shows how to use aggregate functions (COUNT, SUM, AVG, MAX, MIN) with GROUP BY, implement HAVING clause for filtering grouped results, and perform multi-column grouping for comprehensive data analysis.
VIVA QUESTIONS
What is the purpose of GROUP BY clause?
GROUP BY groups rows with the same values in specified columns and allows aggregate functions to be applied to each group.
What is the difference between WHERE and HAVING?
WHERE filters rows before grouping, while HAVING filters groups after GROUP BY operations.
Which aggregate functions can be used with GROUP BY?
COUNT(), SUM(), AVG(), MAX(), MIN(), and other aggregate functions can be used with GROUP BY.
Can you use multiple columns in GROUP BY?
Yes, you can group by multiple columns to create more specific groupings and detailed analysis.
What happens if you use SELECT with non-grouped columns?
In strict SQL mode, you can only SELECT columns that are in GROUP BY clause or use aggregate functions.