TERM 2 Practicals
TERM 2 Practicals
TERM 2 Practicals
Index
Write a menu driven program that allows user to push, pop, peek or
16.
display in a stack.
Create the following table with the data given and perform the
21.
queries given below.
24. Write queries for these questions using the tables given
.Create the table, Teacher. Write query statements for (1) to (4) and
25.
give the outputs.
Q16. Write a menu driven program that allows user to push, pop,
peek or display in a stack.
CODE:-
l=[] mystack=[]
for i in range (10): for i in l:
e=int(input("Enter the numbers: " )) push(mystack,i)
l=l+[e] while True:
print(l) if mystack!=[]:
def push (stack,name): print(pop(mystack),end=" ")
stack.append (name) else:
def pop(stack): break
if stack!=[]: print()
return stack.pop() display(mystack)
else:
return None
def display(stack):
if not stack:
print("Stack is empty ")
else:
top=len(stack)-1
print(stack[top],"<------------TOP")
for a in range( top-1,-1,-1):
print(stack[a])
OUTPUT:-
Q17. Consider the following table named "GYM" with details about fitness
items being sold in the store. Write command of SQL for (i) to (iv).
i)To display the names of all the items whose name starts with "A".
CODE:
OUTPUT:
_____________________________________________________________
_
ii)To display ICODEs and INAMEs of all items, whose Brandname is Reliable
or Coscore.
CODE:
OUTPUT:
_____________________________________________________________
_
iii)To change the Brandname to "Fit Trend India" of the item, who’s ICODE
as "G101".
CODE:
OUTPUT:
_____________________________________________________________
_
iv)Add a new row for new item in GYM with the details :
"G107", "Vibro exerciser", 21000, "GTCFitness
CODE:
OUTPUT:
Table : Pstudents
RollNo Name DOB Gender City
06-
06-
1 Nanda 1995 M Agra
2 Saurabh M Mumbai
06-
05-
3 Sanal 1994 F Delhi
08-
08-
4 Trisla 1995 F Mumbai
08-
10-
Stort 1995 M Delhi
12-
12-
6 Marisla 1994 F Dubai
8-12-
7 Neha 1995 F Moscow
8 12-6-
Nishant 1995 M Moscow
Q18. Write SQL commands for the following based on given tables Pstudent and
Astudent
Table : Astudents
1 Nanda Х 551
3 Sanal XI 400
6 Marisla XI 250
7 Neha X 377
8 Nishant X 489
Code:
Output:
ii)Display name , class and total number of students who have secured
more than 450 marks, class wise.
CODE:
OUTPUT:
___________________________________________________________
___________________________________________________________
OUTPUT:
Q19.Write sql queries for the following based on the tables:
EMPLOYEE and JOB
__________________________________________________________
i)To display employee ids, names of employee, jobid with corresponding
job titles.
CODE:
OUTPUT:
___________________________________________________________
Q20. Create the following table
Table: Salesman
Table : Customer
CODE:
Create table salesman ( salesmanid int(6),Name char(50),City char(50),Commission decimal(4,
2));
Insert into salesman (salesmanid, Name, City, Commission)values (5001, 'James Hoog', 'New
York', 0.15),(5002, 'Nail Knite', 'Paris', 0.13),(5005, 'Pit Alex', 'London', 0.11), (5006, 'Mc Lyon',
'Paris', 0.14),(5007, 'Paul Adam', 'Rome', 0.13),(5003, 'Lauson Hen', 'San Jose', 0.12);
Create table customer (customerid int(7),Custname char(50),City char(50), grade
int(5),salesmanid int(7));
Insert into customer (customerid, Custname, City, Grade, Salesmanid)values(3002, 'Nick
Rimando', 'New York', 100, 5001),(3007, 'Brad Davis', 'New York', 200, 5001),(3005, 'Graham
Zusi', 'California', 200, 5002),(3008, 'Julian Green', 'London', 300, 5002),(3004, 'Fabian Johnson',
'Paris', 300, 5006),(3009, 'Geoff Cameron', 'Berlin', 100, 5003),(3003, 'Jozy Altidor', 'Moscow',
200, 5007),(3001, 'Brad Guzan', 'London', 100, 5005):
i)Display the names of customer with the salesman working with them.
CODE:
OUTPUT:
___________________________________________________________
ii)Display the names of customer whose salesman stays in the same city
as theirs.
CODE:
OUTPUT
iii)Display the names of the salesman and the grade of the customer in
ascending order of the name of salesman.
CODE:
OUTPUT:
___________________________________________________________
CODE:
OU
TPUT:
___________________________________________________________
OUTPUT:
___________________________________________________________________
_
Q21. Create the following table with the data given and perform the queries
given below
CODE:-
CODE:
OUTPUT:
___________________________________________________________________
CODE:
OUTPUT:
___________________________________________________________________
iii)Display the names of customer with their date of joining in descending order
CODE:
OUTPUT:
___________________________________________________________________
iv) Display female customers with accumulated amount greater than 102500.
CODE:
OUTPUT:
___________________________________________________________________
OUTPUT:
___________________________________________________________________
vi)To give boost to spending by females the government wants to add Rs. 5ool000
to all accounts held by females. Write command to do this.
CODE & OUTPUT:
___________________________________________________________________
vii)Add a new column named ‘limit’. This is the limit of one-time withdrawal.
Choose the appropriate data type.
CODE & OUTPUT:
___________________________________________________________________
viii)Delete all the accounts opened before 1 Jan 1999.
CODE & OUTPUT:
Q22. Write a menu driven program to perform crud operation (1. insert record 2.
Read 3. Update 4. delete table) via python. Let the table be the following:
table: EMP
EID ENAME
1001 Raj
1002 Bob
1003 Alia
1004 Dikshita
CODE:-
import mysql.connector
conn = mysql.connector.connect(host="host",user="user",
password="123456",database="MYSQL")
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS EMP
(EID INT PRIMARY KEY,ENAME VARCHAR(50))''')
conn.commit()
def insert_record():
eid = int(input("Enter EID: "))
ename = input("Enter ENAME: ")
cursor.execute("INSERT INTO EMP (EID, ENAME);VALUES (%s, %s)", (eid, ename))
conn.commit()
print("Record inserted successfully.")
def read_records():
cursor.execute("SELECT * FROM EMP")
records = cursor.fetchall()
for record in records:
print("EID:", record[0])
print("ENAME:", record[1])
print()
def update_record():
eid = int(input("Enter EID of the record to update: "))
ename = input("Enter new ENAME: ")
cursor.execute("UPDATE EMP SET ENAME = %s WHERE EID = %s", (ename, eid))
conn.commit()
print("Record updated successfully.")
def delete_table():
confirm = input("Are you sure you want to delete the table? (yes/no): ")
if confirm.lower() == "yes":
cursor.execute("DROP TABLE IF EXISTS EMP")
conn.commit()
print("Table EMP deleted.")
while True:
print("\nMenu:")
print("1. Insert Record")
print("2. Read Records")
print("3. Update Record")
print("4. Delete Table")
print("5. Exit")
if choice == 1:
insert_record()
elif choice == 2:
read_records()
elif choice == 3:
update_record()
elif choice == 4:
delete_table()
elif choice == 5:
print("Exiting...")
break
else:
print("Invalid choice. Please select a valid option.")
conn.close()
OUTPUT:
>>>
Menu:
1. Insert Record
2. Read Records
3. Update Record
4. Delete Table
5. Exit
Enter your choice: 1
Enter EID: 1005
Enter ENAME: Sarah
Record inserted successfully.
Menu:
1. Insert Record
2. Read Records
3. Update Record
4. Delete Table
5. Exit
Enter your choice: 2
EID: 1001
ENAME: Raj
EID: 1002
ENAME: Bob
EID: 1003
ENAME: Alia
EID: 1004
ENAME: Dikshita
EID: 1005
ENAME: Sarah
Menu:
1. Insert Record
2. Read Records
3. Update Record
4. Delete Table
5. Exit
Enter your choice: 3
Enter EID of the record to update: 1002
Enter new ENAME: Robert
Record updated successfully.
Menu:
1. Insert Record
2. Read Records
3. Update Record
4. Delete Table
5. Exit
Enter your choice: 2
EID: 1001
ENAME: Raj
EID: 1002
ENAME: Robert
EID: 1003
ENAME: Alia
EID: 1004
ENAME: Dikshita
EID: 1005
ENAME: Sarah
Menu:
1. Insert Record
2. Read Records
3. Update Record
4. Delete Table
5. Exit
Enter your choice: 4
Are you sure you want to delete the table? (yes/no): yes
Table EMP deleted.
Menu:
1. Insert Record
2. Read Records
3. Update Record
4. Delete Table
5. Exit
Enter your choice: 5
Exiting...
Q23. Create the table Interiors. Write query statements for (1) to (4) and
give the outputs.
CODE:-
CREATE TABLE Interiors (
ITEMNAME VARCHAR(50),
TYPE VARCHAR(50),
DATEOFSTOCK DATE,
DISCOUNT INT);
VALUES
i)To list the ITEMNAME which are priced at more than 10000 from the interiors table
CODE:
SELECT ITEMNAME
FROM Interiors
OUTPUT:
_____________________________________________________________________________________________________________________
ii)To list ITEMNAME and type of those items, in which DATEOFSTOCK is before 22/01/02 from INTERIORS table
in descending order of ITEMNAME.
CODE:
FROM Interiors
OUTPUT:
______________________________________________________________________________________________
iii)To display ITEMNAME and DATEOFSTOCK of those items, in which the DISCOUNT percentage is more than 15 from
INTERIORS table.
CODE:
FROM Interiors
OUTPUT:
______________________________________________________________________________________________
iv)To count the number of items, whose TYPE is “Double Bed” from INTERIORS table
CODE:
FROM Interiors
CODE:-
CODE:
OUTPUT:
CODE:
OUTPUT:
iii) List the sum of the total of orders grouped by customer and state.
CODE:
OUTPUT:
CODE:
OUTPUT:
v) List the customers (name) and the total amount of their orders.
CODE:
OUTPUT:
Q25. Create the table, Teacher. Write query statements for (1) to (4)
and give the outputs.
CODE:-
CREATE TABLE Teacher (
NO INT PRIMARY KEY,
NAME VARCHAR(50),
AGE INT,
DEPARTMENT VARCHAR(50),
DATEOFJOIN DATE,
SALARY DECIMAL(10, 2),
SEX CHAR(1));
INSERT INTO Teacher (NO, NAME, AGE, DEPARTMENT, DATEOFJOIN, SALARY, SEX)
VALUES
(1, 'JUGAL', 34, 'COMPUTER', '1997-10-01', 12000.00, 'M'),
(2, 'SHARMILA', 31, 'HISTORY', '1998-03-24', 20000.00, 'F'),
(3, 'SANDEEP', 32, 'MATHS', '1996-12-12', 30000.00, 'M'),
(4, 'SANGEETA', 35, 'HISTORY', '1999-07-01', 40000.00, 'F'),
(5, 'RAKESH', 42, 'MATHS', '1997-09-05', 25000.00, 'M'),
(6, 'SHYAM', 50, 'HISTORY', '1998-06-27', 30000.00, 'M'),
(7, 'SHIV OM', 44, 'COMPUTER', '1997-02-25', 21000.00, 'M'),
(8, 'SHALAKHA', 33, 'MATHS', '1997-07-31', 20000.00, 'F');
i)To list the names of female teachers who are in History department.
CODE:
OUTPUT:
ii)To list the names of all teachers with their date of joining in ascending order.
CODE:
OUTPUT:
iii)To display teacher’s Name, Salary, Age for male teacher only.
CODE:
OUTPUT:
CODE:
OUTPUT:
v)To insert a new row in the Teacher table with the following data: 9,” Vijay”, 26,” Computer”,
{13/05/95}, 35000,”M”
CODE:
OUTPUT: