Sunday, March 6, 2022

List of Program Question For Class 12 CS - SQL(TERM 2)

12 CS TERM 2

Stack:

  1. Write a Python program to implement a stack using list.
In [1]:
stack=[]
c='y'
while (c=='y'):
    print("1. PUSH")
    print("2. POP")
    print("3. Display")
    choice=int(input("Enter your choices: "))
    if (choice==1):
        a=input("Enter any number: ")
        stack.append(a)
    elif (choice==2):
        if (stack==[]):
            print("Stack Empty")
        else:
            print("Deleted element is :", stack.pop())
    elif (choice==3):
        l=len(stack)
        for i in range(l-1,-1,-1):
            print(stack[i])
    else:
        print("wrong input")
    c=input("Do you want to continue or not? ")
1. PUSH
2. POP
3. Display
Enter your choices: 1
Enter any number: 15
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
Enter your choices: 1
Enter any number: 36
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
Enter your choices: 3
36
15
Do you want to continue or not? n
  1. Write a program to reverse a string using stack.
In [9]:
st=input("Enter the string to reverse ")
ls=list(st)
stack=[]
rvLs=[]
for i in ls:
    stack.append(i)

for i in range(len(stack)):
    rvLs.append(stack.pop())
str=""
for i in rvLs:
    str+=i
print("The string entered: ",st)
print("The string reversed: ",str)
Enter the string to reverse Computer
The string entered:  Computer
The string reversed:  retupmoC
  1. Write a program to create a Stack for storing only odd numbers out of all the numbers entered by the user. Display the content of the Stack along with the largest odd number in the Stack.
In [16]:
val=input("Enter numbers seprated by space")
val=val.split(" ")
for i in range(len(val)):
    val[i]=int(val[i])
stack=[]
for j in val:
    if j%2!=0:
        stack.append(j)
print("The stack of odd numbers are: ",stack)
ref=stack[0]
for i in stack:
    if ref<i:
        ref=i
print("The maximum of the odd stack is: ",ref)
enter numbers seprated by space10 15 79 49 5 7
The stack of odd numbers are:  [15, 79, 49, 5, 7]
The maximum of the odd stack is:  79

SQL Queries:

  1. Create a student table with the student id, class, section, gender, name, dob, andmarks as attributes where the student id is the primary key.

CREATE TABLE student(studentid int(4) primary key, class char(2), section char(1), gender char(1), name varchar(20) dob date, marks decimal(5,2));

img3.JPG

  1. Insert the following data in the student table

question%20practical.JPG

INSERT INTO student VALUES
(1101,'XI','A','M','Aksh','2005/12/23',88.21),
(1102,'XI','B','F','Moksha','2005/03/24',77.90),
(1103,'XI','A','F','Archi','2006/04/21',76.20),
(1104,'XI','B','M','Bhavin','2005/09/15',68.23),
(1105,'XI','C','M','Kevin','2005/08/23',66.33),
(1106,'XI','C','F','Naadiya','2005/10/27',62.33),
(1107,'XI','D','M','Krish','2005/01/23',84.33),
(1108,'XI','D','M','Ayush','2005/04/23',55.33),
(1109,'XI','C','F','Shruti','2005/06/01',74.33),
(1110,'XI','D','F','Shivi','2005/10/19',72.30);

1.JPG

  1. Delete the details of a student in the above table.

DELETE FROM student WHERE studentid=1105;

1.JPG

  1. Use the select command to get the details of the students with marks more than 80.

SELECT * FROM student WHERE marks>80;

1.JPG

  1. Find the min, max, sum, and average of the marks in a student marks table.

SELECT max(marks), min(marks), sum(marks), avg(marks) FROM student;

q5.JPG

  1. Write a SQL query to order the (student ID, marks) table in descending order of the marks.

SELECT * FROM student ORDER BY marks DESC;

q6.JPG

Python - SQL Connectivity.

  1. Write python code to connect to mysql databases.
In [1]:
import mysql.connector as con
connection=con.connect(host="localhost", user="root", password="root")
if connection:
    print("Connection Established")
else:
    print("Connection failed")
    
print(connection)
connection.close()
Connection Established
<mysql.connector.connection.MySQLConnection object at 0x05A91EB0>
  1. Write python code to create a table studentInf given below in mysql database.

q2.JPG

In [4]:
import mysql.connector as con
connection=con.connect(host="localhost", user="root", password="root", database="test")
if connection:
    mycrr=connection.cursor()
    mycrr.execute("CREATE TABLE studentInf(std_code varchar(5), Name varchar(20), school varchar(20), location varchar(20), course_id varchar(6))")
    
else:
    print("Connection failed")
    
print(connection)
connection.close()
<mysql.connector.connection.MySQLConnection object at 0x01030F70>
  1. Write a python progran to insert the following data in the studentInf table.

q3.JPG

In [6]:
import mysql.connector as con
connection=con.connect(host="localhost", user="root", password="root", database="test")

mycursor = connection.cursor()
mycursor.execute("INSERT INTO studentInf values('S01', 'Amit','Lpeak','Shillong','C001')")
mycursor.execute("INSERT INTO studentInf values('S02', 'Meenakshi','Aliganj','Lucknow','C001')")
mycursor.execute("INSERT INTO studentInf values('S03', 'Deep','Happy valley','Shillong','C002')")
mycursor.execute("INSERT INTO studentInf values('S04', 'Shikha','NIT','Agartala','C003')")
mycursor.execute("INSERT INTO studentInf values('S05', 'john','KV','Aizawl','C004')")
mycursor.execute("commit")
connection.close()
  1. Write a python program to display the data stored in studentInf table.
In [10]:
import mysql.connector as con
connection=con.connect(host="localhost", user="root", password="root", database="test")

mycursor = connection.cursor()

mycursor.execute("SELECT * from studentInf")
result=mycursor.fetchall()
for i in result:
    print(i)
connection.close()
('S02', 'Meenakshi', 'Aliganj', 'Lucknow', 'C001')
('S03', 'Deep', 'Happy valley', 'Shillong', 'C002')
('S04', 'Shikha', 'NIT', 'Agartala', 'C003')
('S05', 'john', 'KV', 'Aizawl', 'C004')
('S01', 'Amit', 'Lpeak', 'Shillong', 'C001')
Share: