| WID |
WNAME |
WAGE |
HOURS |
TYPE |
SITEID |
| W01 |
Ahmed J |
1500 |
200 |
Unskilled |
103 |
| W11 |
Naveen S |
520 |
100 |
Skilled |
101 |
| W02 |
Jacob B |
780 |
95 |
Unskilled |
101 |
| W15 |
Nihal K |
560 |
110 |
Semiskilled |
NULL |
| W10 |
Anju S |
1200 |
130 |
Skilled |
103 |
(a) Based on the data given above, answer the following questions:
(i) Write the SQL statement to display the names and wages of those workers whose wages are between 800 and 1500.
Ans: SELECT WNAME, WAGE FROM WORKER WHERE WAGE BETWEEN 800 AND 1500;
(ii) Write the SQL statement to display the record of workers whose SITEID is not known.
Ans: SELECT * FROM WORKER WHERE SITEID IS NULL;
(iii) Write the SQL statement to display WNAME, WAGE and HOURS of all those workers whose TYPE is 'Skilled'.
Ans: SELECT WNAME, WAGE, HOURS FROM WORKER WHERE TYPE="Skilled";
(iv) Write the SQL statement to change the WAGE to 1200 of the workers where the TYPE is "Semiskilled".
Ans: UPDATE WORKER SET WAGE=1200 WHERE TYPE="Semiskilled";
OR
(b) Considering the above given table WORKER, write the output on execution of the following SQL commands:
(i) SELECT WNAME, WAGE HOURS FROM WORKER WHERE SITEID = 103;
| WNAME |
WAGE*HOURS |
| Ahmed J |
300000 |
| Anju S |
156000 |
(ii) SELECT COUNT (DISTINCT TYPE) FROM WORKER;
(iii) SELECT MAX(WAGE), MIN (WAGE), TYPE FROM WORKER GROUP BY TYPE;
| MAX (WAGE) |
MIN (WAGE) |
TYPE |
| 1500 |
780 |
Unskilled |
| 1200 |
520 |
Skilled |
| 560 |
560 |
Semiskilled |
(iv) SELECT WNAME, SITEID FROM WORKER WHERE TYPE="Unskilled" ORDER BY HOURS;
| WNAME |
SITEID |
| Jacob B |
101 |
| Ahmed J |
103 |
33. A csv file "P_record.csv" contains the records of patients in a hospital. Each record of the file contains the following data:
- Name of a patient
- Disease
- Number of days patient is admitted
- Amount
For example, a sample record of the file may be:
["Gunjan", "Jaundice", 4,15000]
Write the following Python functions to perform the specified operations on this file:
(i) Write a function read_data() which reads all the data from the file and displays the details of all the 'Cancer' patients.
Ans:
import csv
def read_data():
F=open("P_record.csv","r")
Records=list(csv.reader(F))
for R in Records :
if R[1]=="Cancer":
print(R)
F.close()
(ii) Write a function count_rec() which counts and returns the number of records in the file.
Ans:
def count_rec():
with open("P_record.csv","r") as F:
Records=list(csv.reader(F))
print(len(Records))
34. Assume that you are working in the IT Department of a Creative Art Gallery (CAG), which sells different forms of art creations like Paintings, Sculptures etc. The data of Art Creations and Artists are kept in tables Articles and Artists respectively. Following are few records from these two tables:
Table: Articles
| Code |
A_Code |
Article |
DOC |
Price |
| PL001 |
A0001 |
Painting |
2018-10-19 |
20000 |
| SC028 |
A0004 |
Sculpture |
2021-01-15 |
16000 |
| QL005 |
A0003 |
Quilling |
2024-04-24 |
3000 |
Table: Artists
| A_Code |
Name |
Phone |
Email |
DOB |
| A0001 |
Roy |
595923 |
r@CrAG.com |
1986-10-12 |
| A0002 |
Ghosh |
1122334 |
ghosh@CrAG.com |
1972-02-05 |
| A0003 |
Gargi |
121212 |
Gargi@CrAG.com |
1996-03-22 |
| A0004 |
Mustafa |
33333333 |
Mf@CrAG.com |
2000-01-01 |
Note: The tables contain many more records than shown here.
DOC is Date of Creation of an Article.
As an employee of CAG, you are required to write the SQL queries for the following:
(i) To display all the records from the Articles table in descending order of price.
Ans: SELECT * FROM Articles ORDER BY PRICE DESC;
(ii) To display the details of Articles which were created in the year 2020.
Ans: SELECT * FROM Articles WHERE DOC LIKE '2020%';
Other Answers:
(i) SELECT * FROM Articles WHERE DOC>='2020-01-01' AND DOC<='2020-12-31';
(ii) SELECT * FROM Articles WHERE DOC BETWEEN '2020-01-01' AND '2020-12-31';
(iii) To display the structure of Artists table.
Ans: DESC Artists;
(iv) (a) To display the name of all artists whose Article is Painting through Equi Join.
Ans: SELECT Name FROM Articles A1, Artists A2 WHERE A1.A_code = A2.A_code AND Article='Painting';
OR
(b) To display the name of all Artists whose Article is 'Painting' through Natural Join.
Ans: SELECT Name FROM Articles NATURAL JOIN Artists WHERE Article = 'Painting';
35. A table, named THEATRE, in CINEMA database, has the following structure:
| Field |
Type |
| Th_ID |
char(5) |
| Name |
varchar(15) |
| City |
varchar(15) |
| Location |
varchar(15) |
| Seats |
int |
Write a function Delete_Theatre (), to input the value of Th_ID from the user and permanently delete the corresponding record from the table.
Assume the following for Python-Database connectivity:
Host: localhost, User: root, Password: Ex2025
Ans:
import pymysql as pm # OR import mysql.connector as pm
def Delete_Theatre():
Mydb=pm.connect(host = 'localhost',
user = 'root', password = 'Ex2025', database = 'CINEMA')
MyCursor = Mydb.cursor()
TID = input("Theatre ID:")
Query = "DELETE FROM Theatre WHERE Th_ID='{}'".format(TID)
MyCursor.execute(Query)
Mydb.commit()
Mydb.close()