Week 3 GQ Solutions
Week 3 GQ Solutions
Week 3
Answer questions 1 and 2 based on the relational schema given in Figure 1.
Solution: The SELECT query looks for those departments which has made a booking
at least once in January, which is achieved by
deptID IN (SELECT deptID FROM hallBooking WHERE monthBooking = ‘Jan’)
and never in February, which is achieved by
deptID NOT in (SELECT deptID FROM hallBooking WHERE
monthBooking = ‘Feb’).
Since both of these conditions have to be satisfied, these two clauses are connected
by AND.
2. Find the names of departments that have booked either hall H0001 or hall H0002 or
both, in both the months of January and February. [MCQ: 2 points]
⃝ SELECT deptName FROM deptsMaster
WHERE deptID IN (SELECT deptID FROM hallBooking
WHERE hallID IN (‘H0001’,‘H0002’)
AND
monthBooking IN (‘Jan’, ‘Feb’));
⃝ SELECT deptName FROM deptsMaster
WHERE deptID IN (SELECT deptID FROM hallBooking
WHERE hallID IN (‘H0001’,‘H0002’)
OR deptID IN
((SELECT deptID FROM hallBooking
WHERE monthBooking=‘Jan’)
UNION
(SELECT deptID FROM hallBooking
WHERE monthBooking=‘Feb’)));
√
SELECT deptName FROM deptsMaster WHERE deptID IN
((SELECT deptID FROM hallBooking WHERE hallID IN
(‘H0001’, ‘H0002’) AND monthBooking = ‘Jan’)
INTERSECT
(SELECT deptID FROM hallBooking WHERE hallID IN
(‘H0001’, ‘H0002’) AND monthBooking = ‘Feb’) )
⃝ SELECT deptName FROM deptsMaster
WHERE deptID IN (SELECT deptID FROM hallBooking
WHERE hallID IN (‘H0001’,‘H0002’)
AND
SELECT deptID FROM hallBooking
WHERE monthBooking=‘Jan’
INTERSECT
SELECT deptID FROM hallBooking
WHERE monthBooking=‘Feb’);
Solution: monthBooking IN (‘Jan’, ‘Feb’) will select all the rows that have ei-
ther January or February as the month of booking.
But, what we need is those departments that have booked in both January and
February and NOT either of them.
In Option 2, the first nested query and the second nested query (with two select
statements connected by an UNION operator) are not looking at rows that satisfy the
condition of having booked in both months. They are individually listing out rows
that satisfy the two conditions, and then the OR is applied. This is not correct.
In Option 4, the same reasoning as that for Option 2 applies.
Page 2
In Option 3, it will take common tuples to both -
SELECT deptID FROM hallBooking
WHERE hallID IN (’H0001’, ’H0002’) AND monthBooking=‘Jan’
INTERSECT
SELECT deptID FROM hallBooking
WHERE hallID IN (’H0001’, ’H0002’) AND monthBooking=‘Feb’
will give deptID of departments that have booked either hall H0001 or hall H0002
or both, in both the months of January and February. And outer query will give
deptName from deptsMaster based on deptID, which is the required set of tuples.
Page 3
3. Consider the relational schema given in Figure 2.
Choose the SQL statement that returns the capitals of all countries that belong to Asia
or Europe. [ MSQ: 2 points]
√
SELECT capitalName FROM capital
WHERE countryID IN (SELECT countryID FROM country
WHERE continent =‘Asia’
UNION
SELECT countryID FROM country
WHERE continent =‘Europe’);
⃝ SELECT capitalName FROM capital
WHERE countryID IN (SELECT countryID FROM country
WHERE continent =‘Asia’
INTERSECT
SELECT countryID FROM country
WHERE continent =‘Europe’);
⃝ SELECT capitalName FROM capital
WHERE countryID NOT IN (SELECT countryID FROM country
WHERE continent =‘Asia’
UNION
SELECT countryID FROM country
WHERE continent =‘Europe’);
√
SELECT capitalName FROM capital
WHERE countryID NOT IN (SELECT countryID FROM country
WHERE continent != ‘Asia’
INTERSECT
SELECT countryID FROM country
WHERE continent != ‘Europe’);
Solution: countryID IN filters in and countryID NOT IN filters out those coun-
tries that satisfy the conditions that follow.
Page 4
In the first option, two select statements are connected by UNION operator in the
inner query. One select statement returns countries that belong to Europe, and the
second select statement returns countries that belong to Asia. Together with union,
the inner query returns all those countries that belong to either Europe or Asia.
In option 2, the connection operator is INTERSECT. This will return only those coun-
tries which belong to both Asia and Europe.
In option 3, countryID NOT IN filters out the countries that belong to either Europe
or Asia.
In option 4, countryID NOT IN filters out the countries that does not belong to Asia
and does not belong to Europe, which effectively filters in those countries that belong
to either Europe or Asia.
Page 5
Use the schema given in Figure 3 to answer the questions 4 to 6.
4. Find the names of employees whose salary is greater than that of every employee in the
department ‘Sales’. [MCQ: 2 points]
⃝ SELECT empName FROM employee E NATURAL JOIN designation D
WHERE E.deptID = (SELECT deptID from department, designation
WHERE deptName = ‘Sales’
AND salary > ALL (SELECT salary
FROM designation D, department T, employee E
WHERE E.desgID = D.desgID AND T.deptID = E.deptID));
⃝ SELECT empName FROM employee E NATURAL JOIN designation D
WHERE D.salary >= ALL (SELECT salary
FROM designation D, department T, employee E
WHERE E.desgID = D.desgID AND T.deptID = E.deptID
AND T.deptName = ‘Sales’);
⃝ SELECT empName FROM employee E NATURAL JOIN designation D
WHERE D.salary > SOME (SELECT salary
FROM designation D, department T, employee E
WHERE E.desgID = D.desgID AND T.deptID = E.deptID
AND T.deptName = ‘Sales’);
√
SELECT empName FROM employee E NATURAL JOIN designation D
WHERE D.salary > ALL (SELECT salary
FROM designation D, department T, employee E
WHERE E.desgID = D.desgID AND T.deptID = E.deptID
AND T.deptName = ‘Sales’);
Page 6
Solution: In option 1, the department name is mentioned in the first inner query.
So, the salary comparison happens between employees of Sales only.
In option 2, ≥ ALL will filter in values that are at least as large as the salary values
returned by the inner query.
In option 3, > SOME will filter in all values that are larger than some salary values
returned by the inner query.
In option 4, > ALL returns the only value that is larger than all values returned by
the inner query. Hence, option 4 is correct.
Page 7
5. What should be filled in the blanks to find the designation of employees with lowest
salary in the Purchase department? [ MCQ: 2 points]
Page 8
6. Based on the relations given in Figure 4, answer the question that follows.
What should be filled in blank (b) in the table employee in Figure 4, if the query given
below returns the value: Lavanya? (Use CAPS for alphabets in the answer) [NAT: 2
points]
Answer: D0001
Solution: The value returned is Lavanya. If we look at the query, it is clear that it
is looking for an employee of department ‘Purchase’ with maximum salary. Clearly,
the deptID should be filled by the deptID of the department ‘Purchase’, which is
‘D0001’ from the table department.
Page 9
7. Consider the relation multiple that stores tuples of the form (a, b) where a is a multiple
of b: [MCQ: 2 points]
If a tuple (a, b) is deleted from multiple, then which of the following is possible?
⃝ a tuple (c, d) such that c is a factor of a is deleted.
√
a tuple (c, d) such that c is a multiple of a is deleted.
⃝ a tuple (c, d) such that d is a factor of a is deleted.
⃝ a tuple (c, d) such that d is a factor of b is deleted.
Solution: The foreign key has been created with ON DELETE CASCADE. When a tuple
(a, b) is deleted from multiple, any tuple of the form (e, a) will be deleted. This
leads to any tuple of the form (d, e) to be deleted, and further any tuple of the form
(c, d) to be deleted. By the property of being a multiple, observe that a is a multiple
of b, e is a multiple of a and b, d is a multiple of e, a and b, and c is a multiple of d,
e, a and b and so on. Thus, a tuple (c, d) such that c is a multiple of a is deleted.
Page 10
8. Consider the relational schema given in Figure 5.
If the relations employee, designation and department have 100, 6, 5 rows respec-
tively, what is the maximum number of rows returned by the following query?
[NAT: 2 points]
Answer: 100
Solution: desgID is the foreign key in table employee that references designation
table. It follows that the desgID in any row of employee table will have a corre-
sponding entry in the designation table. However the converse is not always true.
That is, corresponding to each desgID in the designation table, there need not be
an entry in the employee table. Hence there can only be as many rows in the natural
join as the number of rows in the employee table (since natural join looks for same
value of the common attribute). Thus, the maximum number of rows in the natural
join in the given example is 100.
Page 11
9. Consider the table employee and table department as shown in Figure 6 and answer
the question that follows. [MCQ: 2 points]
⃝ Output:
Page 12
⃝ Output:
√
Output:
Now as per the query, emp id should be IN (6,4,2,7,5) AND age must not be 54 and
it will fetch the following result -
Now as and when we put ORDER BY age in ascending order, it will fetch the
following result -
Page 13
10. Consider table Employee(eid, edept, ename, esalary, ebonus) and the code given below.
[MCQ:2 points]
Page 14
Solution: The trigger is executed for the first insert statement since it satisfies the
condition described in bonus fun. Accordingly the ebonus attribute of the first entry
is set to 75% of the esalary. For the second insert the trigger condition is not satisfied
and hence its ebonus is not set therefore it will remain NULL.
Page 15