Tutorial Sheet 6
Tutorial Sheet 6
Table C:
Id Phone Area
10 2200 02
99 2100 01
SELECT A.id
FROM A
WHERE A.age > ALL (SELECT B.age
FROM B WHERE B. name = \"arun\")
Question 7: The relation scheme given below is used to store information about the
employees of a company, where empId is the key and deptId indicates the department
to which the employee is assigned. Each employee is assigned to exactly one
department.
emp(empId, name, gender, salary, deptId)
Write an SQL query to find the number of female employees in each department whose
salary is higher than the average salary of all employees in the company.
Question 8: Consider the following relational schema:
employee(empId, empName, empDept)
customer(custId, custName, salesRepId, rating)
salesRepId is a foreign key referring to empId of the employee relation. Assume that
each employee makes a sale to at least one customer. Write an SQL query to return the
names of all employees whose customers all have a ‘GOOD’ rating
Question 9: Consider the following relation schema pertaining to a students database:
Student (rollno, name, address)
Enroll (rollno, courseno, coursename)
where the primary keys are shown underlined. The number of tuples in the Student and
Enroll tables are 120 and 8 respectively. What are the maximum and minimum number
of tuples that can be present in (Student * Enroll), where \'*\' denotes natural join ?
Question 10: Consider the following relational schema:
SELECT S.sname
FROM Suppliers S
WHERE S.sid NOT IN (SELECT C.sid
FROM Catalog C
WHERE C.pid NOT IN (SELECT P.pid
FROM Parts P
WHERE P.color<> \'blue\'))
Assume that relations corresponding to the above schema are not empty. What will be
the output of the above query?
Question 11: The STUDENT information in a university stored in the relation STUDENT
(Name, SEX, Marks, DEPT_Name)
Consider the following SQL Query
SELECT DEPT_Name from STUDENT where SEX = \'M\' group by DEPT_Name having avg
(Marks)>SELECT avg (Marks) from STUDENT.
What will the above query returns.
Question 12: Consider the set of relations given below and the SQL query that follows
Students : (Roll number, Name, Date of birth)
Courses: (Course number, Course name, instructor)
Grades: (Roll number, Course number, Grade)
SELECT DISTINCT Name
FROM Students, Courses, Grades
WHERE Students.Roll_number = Grades.Roll_number
AND Courses.Instructor =Sriram
AND Courses.Course_number = Grades.Course_number
AND Grades.Grade = A
What will the above query returns.
Question 13: The relation book (title,price) contains the titles and prices of different
books. Assuming that no two books have the same price, what does the following SQL
query list?
select title from book as B where (select count (*) from book as T where
T.price>B.price)<5
Question 14: Consider the following ORACLE relations : One (x, y) = {<2, 5>, <1, 6>, <1,
6>, <1, 6>, <4, 8>, <4, 8>} Two (x, y) = {<2, 55>, <1, 1>, <4, 4>, <1, 6>, <4, 8>, <4, 8>, <9,
9>, <1, 6>} Consider the following two SQL queries SQ1 and SQ2 :
SQ1 : SELECT * FROM One)
EXCEPT
(SELECT * FROM Two);
SQ2 : SELECT * FROM One)
EXCEPT ALL
(SELECT * FROM Two);
For each of the SQL queries, what is the cardinality (number of rows) of the result
obtained when applied to the instances above?
A) 2 and 1 respectively
B) 1 and 2 respectively
C) 2 and 2 respectively
D) 1 and 1 respectively
Question 15: Consider the following database table named water schemes:
Calculate the number of tuples returned by the following SQL query.
with total(name, capacity) as
select district_name, sum(capacity)
from water_schemes
group by district_name
with total_avg(capacity) as
select avg(capacity)
from total
select name
from total, total_avg
where total.capacity >= total_avg.capacity