0% found this document useful (0 votes)
26 views9 pages

Practical File-Sql Query

The document contains sections on data structures and SQL queries. Section A discusses implementing stack operations like push and pop. Section B provides SQL queries to create and manipulate a database and tables, insert/select/update records, and join tables.

Uploaded by

Taru Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Practical File-Sql Query

The document contains sections on data structures and SQL queries. Section A discusses implementing stack operations like push and pop. Section B provides SQL queries to create and manipulate a database and tables, insert/select/update records, and join tables.

Uploaded by

Taru Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PRACTICAL FILE INDEX

SECTION – A
DATA STRUCTURE – STACK

Each node of a STACK contains the following information:


(i) Pin code of a city, (ii) Name of city
Write a program to implement following operations in above stack
(a) PUSH () to push a node in to the stack.
(b) POP ( ) to remove a rode from the 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 :-

Enter your choice as per given -


1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit

Enter your choice :- insert


Enter the pin code of city :- 100059
Enter name of city :- Delhi
Now our stack = [[100059, 'Delhi']]

Enter your choice as per given -


1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit

Enter your choice :- insert


Enter the pin code of city :- 000000
Enter name of city :- India
Now our stack = [[100059, 'Delhi'], [0, 'India']]

Enter your choice as per given -


1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit

Enter your choice :- insert


Enter the pin code of city :- 168974
Enter name of city :- Lucknow
Now our stack = [[100059, 'Delhi'], [0, 'India'], [168974, 'Lucknow']]

Enter your choice as per given -


1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit

Enter your choice :- delete


Now our stack = [[100059, 'Delhi'], [0, 'India']]

Enter your choice as per given -


1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit

Enter your choice :- delete


Now our stack = [[100059, 'Delhi']]

Enter your choice as per given -


1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit enter exit

Enter your choice :- exit


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Section - B
1. Write SQL query to create and show a database Python.

2. Write SQL query to open database Python.

3. Write a query to create table MENTOR using appropriate constraints on the table.

4. Write a query to show the structure of the table –


5. Write a query to insert records in the table –
> insert into teachers values (1001,"abhay","PGT","ENGLISH", 11, 32, 20000, 950.50,'M');
> insert into teachers values (1002,"AABHA","PRT","HINDI", DEFAULT, 32, 20000, 450.50,'F');
> Insert into teachers values (1003,"MANISH","TGT","COMPUTERS", 12, 52, 30000, 950.34,'M');
> insert into teachers values (1004,"AJAY","PGT","SCIENCE", 12, 35, 35000, 950.50,'M');
> insert into teachers values (1005,"SEEMA","PRT","POEMS", 5, 33, 44000, 250.50,'F');
> insert into teachers values (1006,"MANISHA","TGT","SST", 10, 31, 50000, 950.34,'F');
> insert into teachers (ID, TNAME, SALARY) values (1007,"MAAN", 55000);
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 mentors.

8. Display the name of departments. Each department should be displayed once.


9. Write a 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. 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) :")

You might also like