Practical File-Sql Query
Practical File-Sql Query
SECTION – A
DATA STRUCTURE – STACK
SECTION – B
DATABASE AND SQL QUERIES
1. Write SQL query to create and show a database Python.
2. Write SQL query to open database Python.
3. Write SQL query to create table MENTOR using appropriate constraints on the table.
4. Write SQL query to show the structure of the table.
5. Write SQL query to insert records in the table.
6. Write SQL query to display all the records from table Mentor.
7. Write SQL query to display name, Salary, and Salary added with bonus from table MENTOR.
8. Display the name of departments. Each department should be displayed once.
9. Write SQL query to find total salary paid from mentor.
10. Write SQL query to display all the details from mentor table for those having post
PGT/TGT and gender is male.
11. Write a query to display details of those whose name starts with letter M.
12. Write a query to display names of those having five letters in their name.
13. Write a query to display the details in ascending order of their names.
14. Concatenate name with post for those having salary less than 35000.
15. Write a query to illustrate a difference of now () and sysdate ().
16. Write a query to add a field city in table MENTOR and display its name, post and city.
17. Write a query to add a field city in table MENTOR and display its name, post and city.
18. Write a query to perform join on the tables below –Join – EQUI JOIN & NATURAL JOIN.
19. WAP to connect with database and store record of employee and display records.
20. WAP to connect with database and delete the employee record of entered empno.
Section - A
stack = [ ]
while True :
print()
print("Enter your choice as per given -")
print("1 = For insert data Enter insert ")
print("2 = For delete data enter delete ")
print("3 = For Exit enter exit ")
print()
user = input("Enter your choice :- ")
if user == "insert" :
pin = int(input("Enter the pin code of city :- "))
city = input("Enter name of city :- ")
data = [ pin , city ]
stack.append(data)
elif user == "delete" :
if stack == [ ]:
print("Under Flow")
else :
stack.pop()
else :
break
print("Now our stack = ",stack)
Output :-
3. Write a query to create table MENTOR using appropriate constraints on the table.
7. Write SQL query to display name, Salary, and Salary added with bonus from table mentors.
10. Write SQL query to display all the details from mentor table for those having post
PGT/TGT and gender is male.
11. Write a query to display details of those whose name starts with letter M.
12. Write a query to display names of those having five letters in their name.
13. Write a query to display the details in ascending order of their names.
14. Concatenate name with post for those having salary less than 35000.
15. Write a query to illustrate a difference of now () and sysdate ().
16. Write a query to add a field city in table MENTOR and display its name, post and city.
17. Write a query to add a field city in table MENTOR and display its name, post and city.
18. Write a query to perform join on the tables below –Join – EQUI JOIN & NATURAL JOIN.
19. Program to connect with database and store record of employee and display records.
import mysql.connector as mycon
con = mycon.connect(host="localhost", user="root", passwd="9986")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table if not exists employee(empno int, name varchar(20), dept
varchar(20),salary int)")
con.commit()
choice=None
while choice!=0:
print("1. ADD RECORD ")
print("2. DISPLAY RECORD ")
print("0. EXIT")
choice = int(input("Enter Choice :"))
if choice == 1:
e = int(input("Enter Employee Number :"))
n = input("Enter Name :")
d = input("Enter Department :")
s = int(input("Enter Salary :"))
query="insert into employee values({},'{}','{}',{})".format(e,n,d,s)
cur.execute(query)
con.commit()
print("## Data Saved ##")
elif choice == 2:
query="select * from employee"
cur.execute(query)
result = cur.fetchall()
print("%10s"%"EMPNO","%20s"%"NAME","%15s"%"DEPARTMENT",
"%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
elif choice==0:
con.close()
print("## Bye!! ##")
else:
print("Invalid choice ...")
20. Program to connect with database and delete the employee record of entered empno.
Source Code:-
import mysql.connector as mycon
con = mycon.connect(host='127.0.0.1',user='root',password="root",
database="company") cur = con.cursor()
print("#"*40)
print("EMPLOYEE DELETION FORM")
print("#"*40)
print("\n\n")
ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO DELETE :"))
query="select * from employee where
empno={}".format(eno) cur.execute(query)
result = cur.fetchall()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME", "%15s"%"DEPARTMENT",
"%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
choice=input("\n## ARE YOUR SURE TO DELETE ? (Y) :")
if choice.lower()=='y':
query="delete from employee where
empno={}".format(eno) cur.execute(query)
con.commit()
print("=== RECORD DELETED SUCCESSFULLY! ===")
ans=input("DELETE MORE ? (Y) :")