Documente Academic
Documente Profesional
Documente Cultură
S. NO.
TOPIC
DATE
SIGN.
Question 1
Consider the following relational schema:
Sailors(sid,sname,rating,dote_of_birth,PRIMARY KEY sid)
Boats(bid,bname,color,PRIMARY KEY bid)
Reserves(sid,bid,day,PRIMARY KEY (sid,bid,date))
Boats TABLE:
bid
#100
#101
#102
#103
#104
#105
#106
#107
#108
#109
#110
#111
#112
Bname
Tenda
Bravo
Jungle
Jango
Lampard
Yuhu
Jet
Wilkison
Sasha
Raymond
Raut
Lufth
Heigts
color
White
White
Grey
Black
Red
Yellow
Green
Green
Red
Red
Black
Blue
Blue
Reseves TABLE:
sid
1110
1110
1110
1111
1111
1111
1111
1111
1111
1111
1111
1111
1111
1111
1111
1111
1113
1115
1117
bid
#103
#111
#113
#101
#102
#103
#104
#105
#106
#107
#108
#109
#110
#111
#111
#112
#104
#105
#108
Date
2014-08-09
2014-08-15
2014-05-26
2014-04-29
2014-08-09
2014-05-07
2014-10-01
2014-06-02
2015-07-03
2014-12-09
2014-08-12
2014-04-13
2014-01-18
2014-06-27
2014-12-19
2014-08-23
2014-05-01
2014-03-23
2014-03-08
sid
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
Sname
Ramesh
Rajesh
Rahul
Piyush
Waseem
Javed
Karan
Arun
Vivek
Mithun
Horatio
Sam
Murali
rating
3
1
4
3
2
5
5
3
4
1
7
8
9
dateOfBirth
1984-1-16
1984-5-13
1983-8-12
1985-9-11
1990-1-11
1980-7-3
1987-7-31
1988-9-21
1984-10-6
1991-9-2
1986-3-3
1990-7-3
1982-6-4
sid
1110
1111
1113
1115
1117
1119
1121
Sname
Ramesh
Rajesh
Piyush
Javed
Arun
Mithun
Sam
sid
1111
1113
1117
1119
1121
3. Find sids of sailors whove reserved a red and a green boat
SQL QUERY:
select distinct sid
from reserves,boats
where reserves.bid=boats.bid
and boats.color='Red'and sid in (select distinct sid
from reserves,boats
where reserves.bid=boats.bid
and boats.color='Green');
sid
1111
5. Find sids of sailors who have not reserved a boat
SQL QUERY:
select distinct sid
from sailors
where sid not in (select distinct sid
from reserves,boats
where reserves.bid=boats.bid);
sid
1112
1114
1116
1118
1120
1122
6. Names of sailors whove reserved boat #103
SQL QUERY:
select sname
from sailors,reserves
where sailors.sid=reserves.sid and
reserves.bid='#103';
sname
Ramesh
Rajesh
8. Find sailors whose rating is greater than that of some sailor Horatio
SQL QUERY:
select sid,sname
from sailors
where rating >(select rating
from sailors
where sname='Horatio');
sid
Sname
1121 Sam
1122 Murali
9. Find sailors whove reserved all boats
SQL QUERY:
select reserves.sid
from reserves
group by sid
having count(bid)=13;
sid
1111
10.Find name and age of the oldest sailor(s)
SQL QUERY:
select sname,(TIMESTAMPDIFF(year,dateOfBirth,CURDATE())) as age
from sailors
where TIMESTAMPDIFF(year,dateOfBirth,CURDATE())>=
all(select TIMESTAMPDIFF(year,dateOfBirth,CURDATE())
from sailors);
sname age
Javed 34
11.Find the age of youngest sailor for each rating with atleast 2 such sailors
SQL QUERY:
SELECT rating, min(TIMESTAMPDIFF(year,dateOfBirth,CURDATE())) as age
FROM sailors
GROUP by rating
having count(sid)>=2;
rating
1
3
4
5
age
23
26
30
27
12. Find those ratings for which the average age is the minimum over all ratings
SQL QUERY:
SELECT rating,AVG(TIMESTAMPDIFF(YEAR,dateOfBirth,CURDATE())) as average
FROM sailors
GROUP BY rating
HAVING AVG(TIMESTAMPDIFF(YEAR,dateOfBirth,CURDATE())) <= ALL (SELECT
AVG(TIMESTAMPDIFF(YEAR,dateOfBirth, CURDATE()))
FROM sailors
GROUP BY rating);
rating average
2
24.0000
8
24.0000
Question 2
Consider the following relational schema:
Customer_1(Cust_num PRIMARY KEY,Cust_lname,Cust_fname,Cust_balance)
Customer_2(Cust_num PRIMARY KEY,Cust_lname,Cust_fname,Cust_balance)
Product(Prod_num,PRIMARY KEY,Prod_name,Price)
Vendor(Vendor_id,Location,Vendor_name,Address)
Invoice_1(Inv_num PRIMARY KEY,Prod_num,Unit_sold,cust_num,Inv_date,Inv_amount,Vendor_id)
Customer_1 TABLE:
cust_num
1000
1001
1003
1005
cust_fname
Rakhi
Arun
Simran
Rajiv
cust_lname
Jain
Mehra
Kumari
Khandelwal
cust_bal
3500.00
10000.80
200.00
5000.00
1010
1021
10053
10091
Sumit
Ashutosh
Rashmi
Vipin
Nanda
Gandhi
Anand
Shokeen
2025.80
1000.30
3800.00
73000.00
cust_lname
Jain
Nanda
Singhania
Kashyap
Yadav
Singh
Shokeen
cust_bal
3500.00
2025.80
9000.00
10000.30
13000.30
2900.00
73000.00
Customer_2 TABLE:
cust_num
1000
1010
1015
1029
1043
1052
1091
cust_fname
Rakhi
Sumit
Varun
Ankita
Rolly
Joshi
Vipin
Invoice_1 TABLE:
inv_num
116
123
132
140
156
184
185
187
189
193
199
299
300
536
prod_num
185
134
67
67
67
23109-H
13-Q2/P2
134
23109-H
175
23109-H
58
58
201
unit_sold
2
4
1
3
1
1
2
4
1
2
5
1
1
1
cust_num
1015
1003
1052
1043
1010
1001
1000
1003
1003
1010
1003
1091
1052
1000
Product TABLE:
prod_num
13-Q2/P2
134
175
182
185
201
23109-H
58
6
67
prod_name
Saw
Clock
LAPTOP
Watch
Router
Refrigerator
Hammer
Water Purifier
Washing machine
Television
price
10.00
50.00
800.00
35.00
120.00
300.00
15.00
218.00
200.00
314.00
inv_date
2014-10-12
2014-04-01
2014-05-11
2014-05-18
2014-09-19
2014-10-11
2014-10-10
2014-07-12
2014-08-09
2014-06-05
2014-08-01
2014-07-05
2014-07-07
2014-08-09
inv_amt
240.00
200.00
314.00
942.00
314.00
15.00
20.00
200.00
15.00
1600.00
75.00
218.00
218.00
300.00
vendor_id
113
111
135
112
113
112
113
111
112
133
112
135
136
111
Vendor TABLE:
vendor_id
111
112
113
133
135
136
Location
Delhi
Delhi
Gurgaon
Ghaziabad
Florida
Florida
vendor_name
Mahesh
Anand
Ramesh
Raju
Rick
Scott
vendor_add
A-556,Sarita vihar
A-318,New Ashok Nagar
D-111,Azad Hind Chowk
C-444,Tughalaqabad
Florida
Florida
cust_num
1000
1001
1003
1005
1010
1021
10053
10091
1015
1029
1043
1052
1091
cust_fname
Rakhi
Arun
Simran
Rajiv
Sumit
Ashutosh
Rashmi
Vipin
Varun
Ankita
Rolly
Joshi
Vipin
cust_lname
Jain
Mehra
Kumari
Khandelwal
Nanda
Gandhi
Anand
Shokeen
Singhania
Kashyap
Yadav
Singh
Shokeen
cust_bal
3500
10000.8
200
5000
2025.8
1000.3
3800
73000
9000
10000.3
13000.3
2900
73000
2. Write the query that will show only the duplicate customer records.
SQL QUERY:
SELECT *
FROM customer_1 NATURAL JOIN customer_2;
cust_num
1015
1029
1043
1052
1091
cust_fname
Varun
Ankita
Rolly
Joshi
Vipin
cust_lname
Singhania
Kashyap
Yadav
Singh
Shokeen
cust_bal
9000.00
10000.30
13000.30
2900.00
73000.00
4. Write the query that will show the invoice number, theaverage invoice amount, and the
difference between the average invoice amount and the actual invoice amount.
SQL QUERY:
SELECT inv_num,(SELECT AVG(inv_amt) FROM invoice_1) as average,
((SELECT AVG(inv_amt) FROM invoice_1)-inv_amt) as diff
from invoice_1;
inv_num
116
123
132
140
156
184
185
187
189
193
199
299
300
536
Average
333.642857
333.642857
333.642857
333.642857
333.642857
333.642857
333.642857
333.642857
333.642857
333.642857
333.642857
333.642857
333.642857
333.642857
diff
93.642857
133.642857
19.642857
-608.357143
19.642857
318.642857
313.642857
133.642857
318.642857
-1266.357143
258.642857
115.642857
115.642857
33.642857
5. Modify the Customer table to include two new attributes:Cust_dob and Cust_age. Customer 1000
was born on March 15, 1969 and Customer 1001 was born on December 22,1977.
SQL QUERY:
ALTER TABLE customer_1
ADD cust_dob VARCHAR(20) NULL,
ADD cust_age INT(3) NULL;
ALTER TABLE customer_2
ADD cust_dob VARCHAR(20) NULL,
ADD cust_age INT(3) NULL;
UPDATE customer_1
SET cust_dob='1969-03-15'
WHERE cust_num='1000';
UPDATE customer_2
SET cust_dob='1969-03-15'
WHERE cust_num='1000';
UPDATE customer_1
SET cust_dob='1977-12-22'
WHERE cust_num='1001';
UPDATE customer_1
SET cust_dob='1977-12-22'
WHERE cust_num='1001';
After modification :
Customer_1 table:-
cust_num
1000
1001
1003
1005
1010
1021
10053
10091
cust_fname
Rakhi
Arun
Simran
Rajiv
Sumit
Ashutosh
Rashmi
Vipin
cust_lname
Jain
Mehra
Kumari
Khandelwal
Nanda
Gandhi
Anand
Shokeen
cust_bal
3500.00
10000.80
200.00
5000.00
2025.80
1000.30
3800.00
73000.00
cust_dob
1969-03-15
1977-12-22
NULL
NULL
NULL
NULL
NULL
NULL
cust_age
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
cust_lname
Jain
Nanda
Singhania
Kashyap
Yadav
Singh
Shokeen
cust_bal
3500.00
2025.80
9000.00
10000.30
13000.30
2900.00
73000.00
cust_dob
1969-03-15
NULL
NULL
NULL
NULL
NULL
NULL
cust_age
NULL
NULL
NULL
NULL
NULL
NULL
NULL
Customer_2 table:-
cust_num
1000
1010
1015
1029
1043
1052
1091
cust_fname
Rakhi
Sumit
Varun
Ankita
Rolly
Joshi
Vipin
6. Assuming that the Customer table contains a Cust_age attribute ,write the query to update the
values in this attribute.
SQL QUERY:
UPDATE customer_1
SET cust_age=TIMESTAMPDIFF(YEAR,'1969-03-15',CURDATE())
WHERE cust_num='1000';
UPDATE customer_2
SET cust_age=TIMESTAMPDIFF(YEAR,'1969-03-15',CURDATE())
WHERE cust_num='1000';
UPDATE customer_1
SET cust_age=TIMESTAMPDIFF(YEAR,'1977-12-22',CURDATE())
WHERE cust_num='1001';
UPDATE customer_1
SET cust_age=TIMESTAMPDIFF(YEAR,'1977-12-22',CURDATE())
WHERE cust_num='1001';
After updating:
Customer_1 table:-
cust_age
1000
1001
1003
1005
1010
1021
10053
10091
Rakhi
Arun
Simran
Rajiv
Sumit
Ashutosh
Rashmi
Vipin
Jain
Mehra
Kumari
Khandelwal
Nanda
Gandhi
Anand
Shokeen
3500.00
10000.80
200.00
5000.00
2025.80
1000.30
3800.00
73000.00
1969-03-15
1977-12-22
NULL
NULL
NULL
NULL
NULL
NULL
45
36
NULL
NULL
NULL
NULL
NULL
NULL
cust_lname
Jain
Nanda
Singhania
Kashyap
Yadav
Singh
Shokeen
cust_bal
3500.00
2025.80
9000.00
10000.30
13000.30
2900.00
73000.00
cust_dob
1969-03-15
NULL
NULL
NULL
NULL
NULL
NULL
cust_age
45
NULL
NULL
NULL
NULL
NULL
NULL
Customer_2 table:-
cust_num
1000
1010
1015
1029
1043
1052
1091
cust_fname
Rakhi
Sumit
Varun
Ankita
Rolly
Joshi
Vipin
8. Find all products with a price greater than or equal to the average product price.
SQL QUERY:
select *
from product
where price>(select avg(price)
from product);
prod_num
175
201
58
67
prod_name
LAPTOP
Refrigerator
Water Purifier
Television
price
800.00
300.00
218.00
314.00
9. List all customers who have purchased any kind of hammer or saw.
SQL QUERY:
select distinct cust_num,cust_fname,cust_lname
from customer_1 natural join product natural join invoice_1
where product.prod_name in ('Hammer','Saw');
cust_num
1001
1000
1003
cust_fname
Arun
Rakhi
Simran
cust_lname
Mehra
Jain
Kumari
10. List all products with the total quantity sold greater than the average quantity sold.
SQL QUERY:
select prod_name
from invoice_1,product
where invoice_1.prod_num=product.prod_num
group by prod_name
having sum(unit_sold)>(select avg(unit_sold)
from invoice_1);
prod_name
Clock
Hammer
Television
11. List all products that have product cost that is greater than all individual product costs for
products provided by vendors from Florida.
SQL QUERY:
select prod_name
from product
where price>(select max(price)
from product natural join invoice_1 natural join vendor
where vendor_add ='Florida');
prod_name
LAPTOP
12. Now if we change the problem to list all products that have product cost that is greater than
any individual product costs for products provided by vendors from Florida.
SQL QUERY:
select prod_name
from product
where price>(select min(price)
from product natural join invoice_1 natural join vendor
where vendor_add ='Florida');
prod_name
LAPTOP
Refrigerator
Television
13. Produce a list of all customers who have purchased the products: 13-Q2/P2 and 23109-H.
SQL QUERY:
select distinct cust_num,cust_fname,cust_lname
from customer_1 natural join invoice_1
where prod_num in ('23109-H','13-Q2/P2');
cust_num
1001
1000
1003
cust_fname
Arun
Rakhi
Simran
cust_lname
Mehra
Jain
Kumari
14. Produce a listing of product code ,product price, average price, and the difference between
products price and average products price.
SQL QUERY:
select prod_num,prod_name,price,
(select avg(price) from product) as average_price,
((select avg(price) from product)-price) as difference
from product;
prod_num
13-Q2/P2
134
175
182
185
201
23109-H
58
6
67
prod_name
Saw
Clock
LAPTOP
Watch
Router
Refrigerator
Hammer
Water Purifier
Washing machine
Television
price
10.00
50.00
800.00
35.00
120.00
300.00
15.00
218.00
200.00
314.00
average_price
206.200000
206.200000
206.200000
206.200000
206.200000
206.200000
206.200000
206.200000
206.200000
206.200000
difference
196.200000
156.200000
-593.800000
171.200000
86.200000
-93.800000
191.200000
-11.800000
6.200000
-107.800000
15.Produce a listing of those products in which the units sold for each product is greater than the
average units sold for that product.
SQL QUERY:
select distinct prod_num,prod_name
from product natural join invoice_1
group by prod_name
having sum(unit_sold)>(select avg(unit_sold)
from invoice_1);
prod_num
134
23109-H
67
prod_name
Clock
Hammer
Television
Question 3
Consider the following relational schema:
Location(Loaction_id,Regional Group)
Department(Department_id,Name,Location_id)
Job(Job_id,Function)
Employee(Employee_id, Last_name, First_name, Middle_name, Job_id, Manager_id, Hire_date,
Salary)
Location TABLE:
Location_id
121
122
123
124
125
Regional_Group
Delhi
mumbai
Lucknow
Chennai
Kolkata
Department TABLE:
Department_id
1
10
2
4
5
7
8
name
Trade
External affairs
Human resource
Health
Tourism
Labor
Education
Location_id
121
125
122
123
126
122
124
Job TABLE:
Job_id
21
22
23
24
25
Function
Clerk
Accountant
team leader
sales manager
Trainer
Employee TABLE:
525
Vivek
NULL
Vats
23
13
526
Marry
NULL
Dsouza
23
13
527
Satya
NULL
Yadav
24
13
528
Ram
NULL
Mehra
25
12
529
Shalu
NULL
Sinha
23
14
530
Renu
NULL
Dayal
22
14
566
Rekha
NULL
Kumari
24
15
577
Shyam
NULL
Dixit
21
15
599
Salim
Khan
Sheikh
25
13
19
1985-0409
1985-0413
1988-1230
1985-0311
1988-0101
1987-1118
1990-1213
1991-1307
1985-0429
5200 2
5500 2
5800 2
3900 10
4400 4
4900 6
7800 6
6800 7
6600 2
count(*)
2
3. Which is the department id having greater than or equal to 3 employees joined in April 1985.
SQL QUERY:
select department_id,count(*) as ecount
from
(select *from employee
having extract(year from hire_date)='1985' and
department_id ecount
2
3
department_id name
5
Tourism
8
Education
7. Find out the employees who can earn greater than the average salary of employees of their
department.
SQL QUERY:
select employee_id,first_name,middle_name,last_name,department_id,salary,avg_sal
from (select department_id,avg(salary) as avg_sal
from employee
group by department_id) as salavg natural join employee
where salary>=avg_sal;
employee_id
522
523
524
527
599
529
566
577
first_name
Vipin
Rahul
Vipul
Satya
Salim
Shalu
Rekha
Shyam
middle_name
NULL
NULL
NULL
NULL
Khan
NULL
NULL
NULL
last_name
sharma
Mehta
Garg
Yadav
Sheikh
Sinha
Kumari
Dixit
department_id
1
1
10
2
2
4
6
7
salary
3800
3700
5000
5800
6600
4400
7800
6800
avg_sal
3666.6667
3666.6667
4450.0000
5775.0000
5775.0000
4400.0000
6350.0000
6800.0000
manager_id
521
523
530
count(employee_id)
4
2
4
Employee_id First_name Middle_name Last_name Job_id Manager_id Hire_date Salary Department_id manage
522
Vipin
NULL
sharma
21
523
1983-04- 3800 1
Rahul
09
524
Vipul
NULL
Garg
22
521
1985-03- 5000 10
Lalit
19
525
Vivek
NULL
Vats
23
521
1985-04- 5200 2
Lalit
09
526
Marry
NULL
Dsouza
23
530
1985-04- 5500 2
Renu
527
Satya
NULL
Yadav
24
530
528
Ram
NULL
Mehra
25
521
529
Shalu
NULL
Sinha
23
521
566
Rekha
NULL
Kumari
24
523
577
Shyam
NULL
Dixit
21
530
599
Salim
Khan
Sheikh
25
530
13
1988-1230
1985-0311
1988-0101
1990-1213
1991-1307
1985-0429
5800 2
Renu
3900 10
Lalit
4400 4
Lalit
7800 6
Rahul
6800 7
Renu
6600 2
Renu
Suhaib Khan
776/IT/12
IT-2