Yash - Pratical - File 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Index

S. Questions T.
No Sign
Q21.WAC for following:
21. .WAC for following:
i) to display the sum of sal of all the
employees?
ii) to display the average comm of all the
employee who are either from delhi or
mumbai?
iii) To display the number of employee of
having sal in range 4000 to 5000?
Q22..WAC for following:
22. i) to display the sum of sal for each
city/city-wise form the table EMP?
ii) to display maximum age of the
employee for each design/design-wise
from the table EMP who are from “Delhi”?
iii) To display the number of employee for
each city having sal in range 4000 to 5000
form the table EMP?
Write a SQL query to find all products
23. whose price is higher than the average
price of products in the same category.
Use the products table and explain how
subqueries can be used to derive the
average price within a specific category.
Sql?
Write a SQL query that calculates the total
24. number of orders and the total amount spent by
customers in two different cities, "New York" and
"Los Angeles". Use the customers and orders
tables. Explain how conditional aggregation is
performed in this query.

25.
Write a SQL query to find all products whose
price is higher than the average price of
products in the same category. Use the products
table and explain how subqueries can be used to
derive the average price within a specific
category.

26.
A binary file "STUDENT.DAT" has structure
[admission number, Name, Percentage).
Write a function countrec() in Python that would
read contents of the file "STUDENT.DAT" and
display the details of those students whose
percentage is above 75.

27.
Write a function addrec() in Python to add more
new records at the bottom of a binary file
"STUDENT.dat", assuming the binary file is
containing the following structure

[Roll Number, Student Name]

28.
A binary file “student.dat” has structure
[rollno, name, marks]
Write a user defined function insertRec() to
input data for a student and add to student.dat?

29.
A binary file “student.dat” has structure [rollno,
name, marks]
Write a function searchRollNo( r ) in Python
which accepts the student’s rollno as parameter
and searches the record in the file “student.dat”
and shows the details of student i.e. rollno, name
and marks (if found) otherwise shows the
message as ‘No record found’?

30.
A binary file “emp.dat” has structure [EID,
Ename, designation, salary]. i. Write a user
defined function CreateEmp() to input data for a
record and create a file emp.dat. ii. Write a
function display() in Python to display the detail
of all employees whose salary is more than
50000

Q21.WAC for following:


i)to display the sum of sal of all the employees?
ii)to display the average comm of all the employee who are
either from delhi or mumbai?
iii)To display the number of employee of having sal in range
4000 to 5000?

Ans.
i)Select sum(sal)
From EMP,
ii) select min(age),max(age)
From EMP
Where city=”Delhi”or city=“mumbai”;
iii) select count(.)
From EMP
Where sal between 4000 and 50000;
Q22..WAC for following:
i)to display the sum of sal for each city/city-wise form the table
EMP?
ii) to display maximum age of the employee for each
design/design-wise from the table EMP who are from “Delhi”?
iii)To display the number of employee for each city having sal
in range 4000 to 5000 form the table EMP?

Ans.
i)Select city ,sum(sal)
From EMP
Group by city;
ii)select desig.max(age)
From EMP
Where city=”Delhi”
Group by Desig;
iii)select city, count(.)
From EMP
Where sal between 4000 and 5000
Group by city;

Q23.Write a SQL query to find all products whose price is


higher than the average price of products in the same category.
Use the products table and explain how subqueries can be used
to derive the average price within a specific category.
Sql?
Ans.
SELECT * FROM products WHERE price > (SELECT AVG(price)
FROM products WHERE category = p.category);
Q24.Write a SQL query that calculates the total number of
orders and the total amount spent by customers in two
different cities, "New York" and "Los Angeles". Use the
customers and orders tables. Explain how conditional
aggregation is performed in this query.

Ans.
SELECT c.city,
COUNT(o.order_id) AS total_orders, SUM(o.amount) AS
total_amount_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
WHERE c.city IN ('New York', 'Los Angeles')
GROUP BY c.city;

Q25.Write a SQL query to find all products whose price is


higher than the average price of products in the same category.
Use the products table and explain how subqueries can be used
to derive the average price within a specific category.

Ans.
SELECT * FROM products WHERE price > (SELECT AVG(price)
FROM products WHERE category = p.category);

Q26.A binary file "STUDENT.DAT" has structure [admission


number, Name, Percentage).
Write a function countrec() in Python that would read contents
of the file "STUDENT.DAT" and display the details of those
students whose percentage is above 75.

Ans.
import pickle
def countrec():
fobj=open("student.dat","rb")
num = 0
try:
while True:
pickle.load(fobj)
if rec[2]>75:
num num1
print(rec[0].rec[1].rec[2])
except
fobj.close()

Q27.Write a function addrec() in Python to add more new


records at the bottom of a binary file "STUDENT.dat", assuming
the binary file is containing the following structure
[Roll Number, Student Name]

Ans.
import pickle
def addrec():
fobj=open("student.dat","ab")
rollno=int(input("Roll Number: "))
sname=input("Student Name:")
rec[rolino,sname)
pickle.dump(rec, fobj)
fobj.close()
addrec()

Q28.A binary file “student.dat” has structure


[rollno, name, marks]
Write a user defined function insertRec() to input data for a
student and add to student.dat?

Ans.
import pickle
def insertRec():
f=open("student.dat","ab")
rollno = int (input("Enter Roll Number : "))
name=input("Enter Name :")
marks = int(input("Enter Marks : "))
rec = [rollno, name, marks ]
pickle.dump( rec, f )
f.close()
Q29.A binary file “student.dat” has structure [rollno, name,
marks]
Write a function searchRollNo( r ) in Python which accepts the
student’s rollno as parameter and searches the record in the
file “student.dat” and shows the details of student i.e. rollno,
name and marks (if found) otherwise shows the message as
‘No record found’?

Ans.
def searchRollNo( r ):
f=open("student.dat","rb")
flag = False
while True:
try:
rec=pickle.load(f)
if rec[0] == r :
print(rec[‘Rollno’])
print(rec[‘Name’])
print(rec[‘Marks])
flag == True
except Error:
Break
if flag == False:
print(“No record Found”)
f.close()
Q30.A binary file “emp.dat” has structure [EID, Ename,
designation, salary]. i. Write a user defined function
CreateEmp() to input data for a record and create a file
emp.dat. ii. Write a function display() in Python to display the
detail of all employees whose salary is more than 50000

Ans.
i)import pickle
def CreateEmp():
f1=open("emp.dat",'wb')
eid=input("Enter E. Id")
ename=input("Enter Name")
designation=input("Enter Designation")
salary=int(input("Enter Salary"))
l=[eid,ename,designation,salary]
pickle.dump(l,f1)
f1.close()
ii)import pickle
def display():
f2=open("emp.dat","rb")
try:
while True:
rec=pickle.load(f2)
if rec[3]>5000:
print(rec[0],rec[1],rec[2],rec[3])
except:
f2.close()

You might also like