You are on page 1of 6

CS-262 Problem Set 1

CS262- Database Systems


2021-CS-14 — Fahad Karim Khan
February 16, 2023

1 Solution Problem Set 1


1.1 Problem 01
Find the products(names only) whose cost is more than the average cost.

The Relational Algebra for this problem is:

1.1.1 Cartesian Product


SELECT name
FROM Product P1, (SELECT AVG(cost) AS average FROM Product) as P2
WHERE P1.cost > P2.average

1.1.2 Join Method


SELECT name
FROM Product P1
JOIN (SELECT AVG(cost) AS average FROM Product) as P2
ON P1.cost > P2.average

1.1.3 Sub-Query
SELECT name
FROM Product P1
WHERE cost > (SELECT AVG(cost) FROM Product)

1.1.4 Except Query


SELECT name
FROM Product
WHERE name not in (SELECT name FROM Product
EXCEPT

1
SELECT name FROM Product WHERE cost > (select AVG(cost) from Product ));

1.2 Problem 02
List the name of companies whose products are bought by Aslam.

The Relational Algebra for this problem is:

1.2.1 Cartesian Product


SELECT name
FROM Company C, Purchase P
WHERE C.name = P.product AND P.buyer = ’Aslam’

1.2.2 Join Method


SELECT NAME
FROM Company C
JOIN Purchase P
ON C.name = P.product AND P.buyer = ’Aslam’

1.2.3 Sub-Query
SELECT name
FROM Company
WHERE name IN (SELECT product FROM Purchase WHERE buyer = ’Aslam’)

1.3 Problem 03
List the name of products that are more expensive that all the products produced by Unilever.

The Relational Algebra for this problem is:

2
1.3.1 Cartesian Product
SELECT name
FROM Product P1, (SELECT SUM(COST) AS Total FROM Product WHERE maker = ’Unilever’) AS P2
WHERE P1.cost >P2.Total

1.3.2 Join Method


SELECT name
FROM Product P1
JOIN (SELECT SUM(COST) AS Total FROM Product WHERE maker = ’Unilever’) AS P2

1.3.3 Sub-Query
SELECT name
FROM Product P1
WHERE P1.cost >(SELECT SUM(COST) AS Total FROM Product P2 WHERE P2.maker=’Unilever’)

1.4 Problem 04
List the copy cat products along with manufacturer, i.e. the products that have the same name as produced
by Unilever.

The Relational Algebra for this problem is:

1.4.1 Group By Method


SELECT name,maker
FROM Product P1
GROUP BY name,maker
HAVING COUNT(P1.name) > 1 AND COUNT(P1.maker) > 1

1.5 Problem 05
Buyers of products produced in Lahore.

The Relational Algebra for this problem is:

3
1.5.1 Cartesian Product
SELECT buyer
FROM Purchase P1 ,(SELECT name as Cname FROM Company WHERE city =’INDIGO’) AS P2
WHERE P1.product =P2.Cname

1.5.2 Join Method


SELECT buyer
FROM Purchase P1
JOIN (SELECT name as Cname FROM Company WHERE city =’INDIGO’) AS P2 ON P1.product
=P2.Cname

1.5.3 Sub-Query
SELECT buyer
FROM Purchase P1
WHERE P1.product IN (SELECT name FROM Company WHERE city =’INDIGO’)

1.6 Problem 06
List of buyers, who only buy the products ’Made in Karachi’.

The Relational Algebra for this problem is:

1.6.1 Cartesian Product


SELECT buyer
FROM Purchase P1,(SELECT name FROM Company C WHERE C.city =’INDIGO’) AS C
WHERE P1.product =C.name

1.6.2 Join Method


SELECT buyer
FROM Purchase P1
JOIN (SELECT name FROM Company C WHERE C.city =’INDIGO’) AS C
ON P1.product =C.name

1.6.3 Sub-Query
SELECT buyer
FROM Purchase P1
WHERE P1.product IN (SELECT name FROM Company C WHERE C.city =’INDIGO’)

4
1.7 Problem 07
Name and price of products bought by more than five customers.

The Relational Algebra for this problem is:

1.7.1 Cartesian Product


SELECT DISTINCT Product
FROM Purchase P1 , (SELECT product As Result FROM Purchase GROUP BY product HAVING COUNT(product)
> 4) As P2
WHERE P1.product = P2.Result;

1.7.2 Join Method


SELECT DISTINCT Product
FROM Purchase P1 join (SELECT product As Result FROM Purchase GROUP BY product HAVING
COUNT(product) > 4) As P2
ON P1.product = P2.Result;

1.7.3 Sub-Query
SELECT DISTINCT Product
FROM Purchase
WHERE product in(SELECT product FROM Purchase GROUP BY product HAVING COUNT(product)
> 4) ;

1.7.4 Group By
SELECT product
FROM Purchase
GROUP BY product
HAVING COUNT(product) >= 5

1.7.5 Except Query


SELECT DISTINCT Product
FROM Purchase
EXCEPT

5
SELECT product
FROM Purchase GROUP BY product
HAVING COUNT(product) < 4;

1.8 Problem 08
Name and price of products bought by more than five customers.

1.8.1 Sub-Query
SELECT name, maker, cost, year
FROM Product p1
WHERE cost < (SELECT MAX(cost) FROM Product p2
WHERE p1.maker = p2.maker ) AND p1.year = 2015

You might also like