TERM 2 Practicals

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

TERM – 2

Index

Write a menu driven program that allows user to push, pop, peek or
16.
display in a stack.

Consider the following table named "GYM" with details


17. about fitness items being sold in the store. Write command of SQL
for (i) to (iv).

Write SQL commands for the following based on given


18.
tables Pstudent and Astudent

Write sql queries for the following based on the tables:


19.
EMPLOYEE and JOB

20. Create the tables “Salesman” and “Customer”

Create the following table with the data given and perform the
21.
queries given below.

Write a menu driven program to perform crud operation (1.


22. insert record 2. Read 3. Update 4. delete table) via python. Let the
table be the following:
Create the table Interiors. Write query statements for (1) to (4) and
23.
give the outputs.

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

RollNo Name Class Marks

1 Nanda Х 551

2 Saurabh XII 462

3 Sanal XI 400

4 Trisla XII 450

5 Stort XII 369

6 Marisla XI 250

7 Neha X 377

8 Nishant X 489

i)increase marks of all students by 20 whose class is XI.

Code:
Output:

ii)Display name , class and total number of students who have secured
more than 450 marks, class wise.

CODE:

OUTPUT:

___________________________________________________________

iii)Count the number of boys and girls in grade XII

CODE and OUTPUT:

___________________________________________________________

iv)Display the records in Pstudent table in alphabetical order as per the


name of the students
CODE:

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:

___________________________________________________________

iv)Display number of salesman from each city.

CODE:

OU
TPUT:

___________________________________________________________

v)Display maximum increase in the commission of salesman having


customer in ‘new york’ by 10%.
CODE:

OUTPUT:

___________________________________________________________________
_

Q21. Create the following table with the data given and perform the queries
given below
CODE:-

Create table CUSTOMER_DETAILS (CUST_ID varchar(10),CUT_NAME char(10),


ACC_TYP char(10),ACCULT_TYP int(10),DOJ date,GENDER char(2));
Insert into CUSTOMER_DETAILS(CUST_NAME, CUT_NAME, ACC_TYP,
ACCULT_TYP, DOJ,GENDER)
values
(CNR_101,manoj,saving,1025000,’1999-02-19’,M’’),
(CNR_102, 'rahul,’Current’ 1326000, '1998-01-11',M’’),
(CNR_103, 'john', 'saving', 1425000, '1999-02-04'’M’),
(CNR_104, 'steve', 'salary ', 1825000, '1998-02-12’M’),
(CNR_105, 'manpreet', 'saving', 1125000, '1998-05-12',’F’),
(CNR_106, 'catherine', ‘saving', 1026000, ‘1999-01-13',’F’),
(CNR_107,’ramesh’,’saving’,2025000,’1998-04-22’,’M’);
i)Display the amount and date of joining of savings accounts.

CODE:

OUTPUT:

___________________________________________________________________

ii)Display name and gender of current account holders.

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:

___________________________________________________________________

v)Display the customer having 5 letters' names.


CODE:

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")

choice = int(input("Enter your choice: "))

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 (

No INT PRIMARY KEY,

ITEMNAME VARCHAR(50),

TYPE VARCHAR(50),

DATEOFSTOCK DATE,

PRICE DECIMAL(10, 2),

DISCOUNT INT);

INSERT INTO Interiors (No, ITEMNAME, TYPE, DATEOFSTOCK, PRICE, DISCOUNT)

VALUES

(1, 'RED ROSE', 'DOUBLE BED', '2002-02-23', 32000.00, 15),

(2, 'SOFT TOUCH', 'BABY COT', '2002-01-20', 9000.00, 10),

(3, 'JERRY’S HOME', 'BABY COT', '2002-02-19', 8500.00, 10),

(4, 'ROUGH WOOD', 'OFFICE TABLE', '2002-01-01', 20000.00, 20),

(5, 'COMFORT ZONE', 'DOUBLE BED', '2002-01-12', 15000.00, 20),

(6, 'JERRY LOOK', 'BABY COT', '2002-02-24', 7000.00, 19),

(7, 'LION KING', 'OFFICE TABLE', '2002-02-20', 16000.00, 20),

(8, 'ROYAL TIGER', 'SOFA', '2002-02-22', 30000.00, 25),

(9, 'PARK SITTING', 'SOFA', '2001-12-13', 9000.00, 15),

(10, 'DINE PARADISE', 'DINING TABLE', '2002-02-19', 11000.00, 15);

i)To list the ITEMNAME which are priced at more than 10000 from the interiors table

CODE:

SELECT ITEMNAME

FROM Interiors

WHERE PRICE > 10000;

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:

SELECT ITEMNAME, TYPE

FROM Interiors

WHERE DATEOFSTOCK < '2002-01-22'

ORDER BY ITEMNAME DESC;

OUTPUT:

______________________________________________________________________________________________

iii)To display ITEMNAME and DATEOFSTOCK of those items, in which the DISCOUNT percentage is more than 15 from
INTERIORS table.

CODE:

SELECT ITEMNAME, DATEOFSTOCK

FROM Interiors

WHERE DISCOUNT > 15;

OUTPUT:
______________________________________________________________________________________________

iv)To count the number of items, whose TYPE is “Double Bed” from INTERIORS table

CODE:

SELECT COUNT(*) AS CountOfDoubleBeds

FROM Interiors

WHERE TYPE = 'DOUBLE BED';


OUTPUT:
Q24. Write queries for these questions using the tables given below:

CODE:-

i)List the total of customers' orders grouped by customer id.

CODE:

OUTPUT:

ii) List all the customers (name) who have orders.

CODE:
OUTPUT:

iii) List the sum of the total of orders grouped by customer and state.

CODE:

OUTPUT:

iv) List the customers (name) and their orders' details.

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:

iv)To count the number of teachers with Age >23.

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:

You might also like