Gagan Bansal (CS Practical File)
Gagan Bansal (CS Practical File)
Gagan Bansal (CS Practical File)
COMPUTER SCIENCE
SESSION-: 2024-25
a) To display the total unit price of all the products whose dcode is 102.
b) To display the product name of those items whose name contains “e” as
second alphabet in their name.
c) To display the details of all the products in the stock table in
descending order of stock date.
d) To display the maximum unit price of products for each dcode.
e) To display product name and dealer name from table stock and dealers.
3. Consider the following tables ITEM and CUSTOMER. Write SQL query for the
following statements:
4. Consider the following tables INTERIORS and ARRIVALS. Write SQL queries based
on the following tables:
a)To show all information about the baby cot’s from the interiors table.
b)To list item name and type of those items, in which dateofstock is before 22/01/02
from the interiors table in descending order of itemname.
c) To display itemname and date stock of OFFICE TABLE items, in which the
discount is more than 15 from the interiors table.
d)To count the number of rows, whose type is “Double Bed” from the INTERIORS
table.
e)To insert a new row in the Arrivals table with the following data:
14,”True Indian”,”Office table”,”28/03/03”,15000,20
5 Consider the following tables ACTIVITY and COACH. Write SQL queries for the
following:
a)To display the names of all activities with their Acodes in descending order.
b)To display sum of prizemoney for the activities played in each of the stadium
separately.
c)To display the activity name and coach name from the above tables.
d)To display the content of all the activities for which scheduledate is earlier than
2004-01-01 in ascending order of participantsnum.
6. Kabir wants to write a program in Python to insert the following record in the table
name STUDENT in MySQL database, SCHOOL:
● Rno(Roll no)-integer
● Name-string
● DOB(Date of birth)-Date
● Fee-float
Note the following to establish connectivity between Python and MySQL:
⮚ Username-root
⮚ Password-tiger
⮚ Host-localhost
The values of fields rno, name, DOB and fee are to be accepted by the user. Help
Kabir to write the program in Python.
7 The table employee contains the following structure:
● E-code-string
● E_name-string
● Sal-integer
● City-string
To display all the records from the employee table in Python.
Note the following to establish connectivity between Python and MySQL:
Username-root
Password-root
The table exists in a MySQL database named emp.
8 The table employee contains the following structure:
● E-code-string
● E_name-string
● Sal-integer
● City-string
To delete the record from thew table with Ecode as “E101”.
Note the following to establish connectivity between Python and MySQL:
Username-root
Password-root
The table exists in a MySQL database named emp.
9 The table employee contains the following structure:
● E-code-string
● E_name-string
● Sal-integer
● City-string
To increase the salary of the employee from the table with Ecode “E101” by 500 and
make the changes permanent.
Note the following to establish connectivity between Python and MySQL:
Username-root
Password-root
The table exists in a MySQL database named emp.
1 Amit wants to write a program in Python to display the details of the records where
0 marks>75 in the table name STUDENT in MySQL database, SCHOOL:
● Rno(Roll no)-integer
● Name-string
● Class-integer
● Marks-integer
Note the following to establish connectivity between Python and MySQL:
⮚ Username-root
⮚ Password-tiger
⮚ Host-localhost
Help Amit to write the program in Python.
SOLUTIONS
Q.NO SOLUTION
6 import mysql.connector
from mysql.connector import Error
def insert_student(rno, name, dob, fee):
connection = mysql.connector.connect(
host='localhost',
database='SCHOOL',
user='root',
password='tiger'
)
if connection.is_connected():
cursor = connection.cursor()
cursor.execute(insert_query, record)
connection.commit()
except Error as e:
print("Error while connecting to MySQL", e)
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
7 import mysql.connector
connection = mysql.connector.connect(
host='localhost',
user='root',
password='root',
database='emp'
)
cursor = connection.cursor()
records = cursor.fetchall()
for record in records:
print(record)
cursor.close()
connection.close()
8 import mysql.connector
connection = mysql.connector.connect(
host='localhost',
user='root',
password='root',
database='emp'
)
cursor = connection.cursor()
cursor.execute(delete_query)
connection.commit()
if cursor:
cursor.close()
if connection:
connection.close()
9 import mysql.connector
connection = mysql.connector.connect(
host='localhost',
user='root',
password='root',
database='emp'
)
cursor = connection.cursor()
update_query = """
UPDATE employee
SET Sal = Sal + 500
WHERE E-code = 'E101'
"""
cursor.execute(update_query)
connection.commit()
if cursor:
cursor.close()
if connection:
connection.close()
10 import mysql.connector
def get_students_with_high_marks():
connection = mysql.connector.connect(
host='localhost',
user='root',
password='tiger',
database='SCHOOL'
)
cursor = connection.cursor()
cursor.execute(query)
result = cursor.fetchall()
if result:
print("Records with marks greater than 75:")
for row in result:
print(f"Roll No: {row[0]}, Name: {row[1]}, Class: {row[2]}, Marks: {row[3]}")
else:
print("No records found with marks greater than 75.")
cursor.close()
connection.close()
print("MySQL connection is closed.")
get_students_with_high_marks()