ID NAME AGE CITY FEE PHONE
P1 SAMEER 34 DELHI 45000 9811076656
P2 ARYAN 35 MUMBAI 54000 9911343989
P4 RAM 34 CHENNAI 45000 9810593578
P6 PREMLATA 36 BHOPAL 60000 9910139987
Table COACHING is shown below. Write commands in SQL for (i) to (iii) and show the output for (iv)
and (v) (5)
P7 SHIKHA 36 INDORE 34000 9912139456
P8 RADHA 33 DELHI 23000 8110668888
(i)Write a query to display name in descending order whose age is more than 23.
Ans.
select name from coaching where age>23 order by name desc;
(ii)Write a query to find the average fee grouped by age from customer table.
Ans.
select avg(fee) from coaching group by age;
(iii) Write query details from coaching table where fee is between 30000 and 40000.
Ans.
Select * from coaching table where fee is between 30000 and 40000;
(iv)Select sum(Fee) from coaching where city like “%O% ;
Ans.
94000
(v)Select name, city from coaching group by age having count(age)>2;
Ans.
Empty set
2.Observe the table ‘Club’ given below:
Club
i.What is the cardinality and degree of the above given table?
ii. If a new column contact_no has been added and three more members have joined the club then
how these changes will affect the degree and cardinality of the above given table.
Ans i. Cadinality: 4
Degree: 5
ii. Cardinality: 7
Degree: 6
3.Write SQL commands for the queries (i) to (iv) and output for (v) to (viii) based on the tables
‘Watches’ and Sale given below.
i.To display all the details of those watches whose name ends with ‘Time’
ii. To display watch’s name and price of those watches which have price range in between 5000-
15000.
iii. To display total quantity in store of Unisex type watches.
iv. To display watch name and their quantity sold in first quarter.
v. select max(price), min(qty_store) from watches;
vi. select quarter, sum(qty_sold) from sale group by quarter;
vii. select watch_name,price,type from watches w, sale s where w.watchid!=s.watchid;
viii. select watch_name, qty_store, sum(qty_sold), qty_store- sum(qty_sold) “Stock” from watches w,
sale s where w.watchid=s.watchid group by s.watchid;
i. select * from watches where watch_name like ‘%Time’
ii. select watch_name, price from watches where price between 5000 and 15000;
iii. select sum(qty_store) from watches where type like ’Unisex’;
4. (a) Observe the following table MEMBER carefully and write the name
of the RDBMS operation out of (i) SELECTION (ii) PROJECTION (iii)
UNION (iv) CARTESIAN PRODUCT, which has been used to produce the
output as shown in RESULT. Also, find the Degree and Cardinality of the
RESULT: (2)
MEMBER
NO MNAME STREA
M
M001 JAYA SCIENC
E
M002 ADITYA HUMAN
ITIES
M003 HANSR SCIENC
AJ E
M004 SHIVAK COMMER
CE
RESULT
NO MNAM STREA
E M
M002 ADITYA HUMA
NITIES
Ans. (i) SELECTION
Degree=3 Cardinality=1
5. Write SQL queries for (i) to (iv) and find outputs for SQL queries (v)
to (viii), which are based on the tables. (6)
DVD
(i) To display all details from the table MEMBER in descending order of
ISSUEDATE. Ans. SELECT * FROM MEMBER ORDER BY ISSUEDATE DESC;
(ii) To display the DCODE and DTITLE of all Folk Type DVDs from the
table DVD. Ans. SELECT DCODE,DTITLE FROM DVD WHERE DTYPE=’Folk’;
(iii) To display the DTYPE and number of DVDs in each DTYPE from the
table DVD. Ans. SELECT COUNT(*),DTYPE FROM DVD GROUP BY DTYPE;
(iv) To display all NAME and ISSUEDATE of those members from the
table MEMBER
who have DVDs issued (i.e., ISSUEDATE) in the year 2017. Ans. SELECT
NAME, ISSUEDATE FROM MEMBER WHERE ISSUEDATE>=’2017-01-01’
AND ISSUEDATE<=’2017-12-31’;
OR SELECT NAME, ISSUEDATE FROM MEMBER WHERE ISSUEDATE
BETWEEN ‘2017-01-01’ AND ‘2017-12-31’;
OR SELECT NAME, ISSUEDATE FROM MEMBER WHERE ISSUEDATE LIKE
‘2017%’;
(v) SELECT MIN(ISSUEDATE) FROM MEMBER; Ans. MIN(ISSUEDATE)
2016-12-13
(vi) SELECT DISTINCT DTYPE FROM DVD; Ans. DISTINCT DTYPE
Folk Classical Rock
(vii) SELECT D.DCODE, NAME, DTITLE
FROM DVD D, MEMBER M WHERE D.DCODE=M.DCODE;
Ans.
DCODE NAME DTITLE
R102 AGAM A Day in
SINGH Life
R102 ARTH Universal
JOSEPH Soldier
C101 NISHA The
HANS Planets
(viii) SELECT DTITLE FROM DVD
WHERE DTYPE NOT IN ("Folk", "Classical"); Ans. DTITle
A day in life
6. Write SQL queries for (i) to (iv) and find outputs for SQL queries (v)
to (viii), which are based on the tables.
(i) To display CNO, CNAME, TRAVELDATE from the table TRAVEL in descending order of CNO.
Ans SELECT CNO, CNAME, TRAVELDATE FROM TRAVEL ORDER BY CNO
DESC;
(ii) To display the CNAME of all the customers from the table TRAVEL who are traveling by vehicle with code V01
or V02.
Ans SELECT CNAME FROM TRAVEL WHERE VCODE=‘V01’ OR VCODE=’V02’;
OR
SELECT CNAME FROM TRAVEL WHERE VCODE IN (‘V01’, ‘V02’);
(iii) To display the CNO and CNAME of those customers from the table TRAVEL who travelled between ‘2015 ‐12 ‐
31’ and ‘2015‐05‐01’.
Ans SELECT CNO, CNAME from TRAVEL WHERE TRAVELDATE >= ‘20150501’ AND TRAVELDATE <= ‘20151231’ ;
OR
SELECT CNO, CNAME from TRAVEL WHERE TRAVELDATE BETWEEN ‘20150501’
AND ‘20151231’ ;
OR
SELECT CNO, CNAME from TRAVEL WHERE TRAVELDATE <= ‘20151231’
AND TRAVELDATE >= ‘20150501’ ;
OR
SELECT CNO, CNAME from TRAVEL WHERE TRAVELDATE BETWEEN ‘20151231’
AND ‘20150501’ ;
(iv) To display all the details from table TRAVEL for the customers, who have travel
distance more than 120 KM in ascending order of NOP.
Ans SELECT * FROM TRAVEL WHERE KM > 120 ORDER BY NOP;
(v)SELECT COUNT(*),VCODE FROM TRAVEL GROUP BY VCODE HAVING COUNT(*)>1;
(
Count(*) Vcode
2 v01
2 vo2
(vi) SELECT DISTINCT VCODE FROM TRAVEL;
Distinct vcode
Vo1
Vo2
Vo3
Vo4
Vo5
(a) Write a MYSQL-Python connectivity code to retrieve one record at a time from table EMPLOYEES
who live in ‘Delhi’
(b) Consider the tables given below: -
STAFF
StaffID Name Gender Experience
Department
1125 Nihara Sales F 12
1263 Kartik Finance M 6
1452 Payal Research F 3
236 Aryan Sales M 8
366 Laxman Finance M 10
321 Krishna Sales M 7
SALARY
StaffID Basic Allowance Comm
1452 12000 1000 200
321 23000 2300 900
1125 32000 4000 100
236 12000 52000 800
336 42000 1700 700
1263 18900 1690 150
With reference to the above table, write commands in SQL for (i) to (iv) and output for (v) to (viii)
(i) To display names of all staff that are in Sales department having experience less than 9 years and
commission more than 700
(ii) Display average salary of staff working in Finance department. Salary= Basic+Allowance
(iii) Display number of female members in each department.
(iv) Display name of employee earning maximum salary.
Give Output
(v) SELECT NAME FROM STAFF ST, SALARY SA WHERE COMM <=700 AND ST.STAFFID = SA.STAFFID
(vi) SELECT NAME, BASIC FROM STAFF, SALARY WHERE DEPT=”SALES” AND STAFF.STAFFID=
SALARY.STAFFID
(vii) SELECT COUNT(DEPARTMENT),DEPARTMENT FROM STAFF GROUP BY DEPARTMENT
(viii) SELECT COUNT(*),GENDER FROM STAFF GROPU BY GENDER
(1) select name from staff st, salary sl
where st.staffid = sl.staffid and
dept=’sales’ and experience<9 and comm>700;
(2) select avg(basic+allowance) as ‘average salary’
from staff st, salary sl
where st.staffid=sl.staffid and dept=’finance’;
(3) select gender, count(gender)
from staff group by gender;
(4) select name from staff st, salary sl
where (sl.basic+sl.allowance) = (select max(sl.basic+sl.allowance)
from salary);
Write SQL commands for the queries (i) to (iv) and output for (v) & (viii) based
on a table COMPANY and CUSTOMER
COMPANY
CID NAME CITY
PRODUCT
NAME
111 SONY DELHI TV
222 NOKIA MUMBAI MOBILE
333 ONIDA DELHI TV
444 SONY MUMBAI MOBILE
555 MADRAS MOBILE
BLACKBERRY
666 DELL DELHI LAPTOP
CUSTOMER
CUSTID NAME PRICE QTY CID
101 Rohan 70000 20 222
Sharma
102 Deepak 50000 10 666
Kumar
103 Mohan 30000 5 111
Kumar
104 Sahil 35000 3 333
Bansal
105 Neha Soni 25000 7 444
106 Sonal 20000 5 333
Aggarwal
107 Arjun Singh 50000 15 666
(i) To display those company name which are having price less than 30000.
(ii) To display the name of the companies in reverse alphabetical order.
(iii) To increase the price by 1000 for those customer whose name starts with ‘S’
(iv) To add one more column totalprice with decimal(10,2) to the table customer
(v) SELECT COUNT(*) ,CITY FROM COMPANY GROUP BY CITY;
(vi) SELECT MIN(PRICE), MAX(PRICE) FROM CUSTOMER WHERE QTY>10 ;
(vii) SELECT AVG(QTY) FROM CUSTOMER WHERE NAME LIKE “%r%;
(viii) SELECT PRODUCTNAME,CITY, PRICE FROM COMPANY,CUSTOMER
WHERE COMPANY.CID=CUSTOMER.CID AND PRODUCTNAME=”MOBILE”;
(i) select name from customer where price <30000;
(ii) select name from company order by name desc.
(iii) update customer set price=price+1000 where name like “S%”;
(iv) alter table customer add totalprice decimal(10,2);
(v) count(*) city
3 DELHI
2 MUMBAI
1 MADRAS
(vi) MIN(PRICE) MAX(PRICE)
50000 70000
(vii) AVG(QTY)
12
(viii) PRODUCTNAME CITY PRICE
MOBILE MUMBAI 70000
MOBILE MUMBAI 25000