0% found this document useful (0 votes)
2 views

Tutorial Sheet 6

The document contains a series of SQL-related questions and queries, covering topics such as joins, aggregations, and subqueries. Each question requires the reader to analyze SQL statements and determine their outputs or results based on given data. The questions involve various database schemas and require knowledge of SQL syntax and operations.

Uploaded by

Jagrati Kaushik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Tutorial Sheet 6

The document contains a series of SQL-related questions and queries, covering topics such as joins, aggregations, and subqueries. Each question requires the reader to analyze SQL statements and determine their outputs or results based on given data. The questions involve various database schemas and require knowledge of SQL syntax and operations.

Uploaded by

Jagrati Kaushik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Tutorial Sheet 6

Question 1: Consider the SQL query below:


select deptId, count (*)
from emp
where gender = “female” and salary > (select avg(salary)from emp)
group by deptId;
What will be the output of the above query?
Question 2: Suppose ORACLE relation R (A, B) currently has tuples {(1, 2), (1, 3), (3, 4)}
and relation S (B, C) currently has {(2, 5), (4, 6), (7, 8)}. Consider the following two SQL
queries SQ1 and SQ2:
SQ1: Select * From R Full Join S On R.B = S.B;
SQ2: Select * From R Inner Join S On R.B = S.B.
Calculate the numbers of tuples in the result of the SQL query SQ1 and the SQL query
SQ2.
Question 3: STUDENT (Name, Gender, Marks, DEPT Name) is a relation that stores
STUDENT information at a university. Consider the SQL Query below.
SELECT DEPT Name
from STUDENT
where Gender = 'M'> GROUP BY DEPT_Name having avg(Marks)>SELECT avg (Marks)
from STUDENT.
What will be the output of the query?
Question 4: Consider the following tables.
Write an SQL query to find the third highest salary in a table?”
Question 5: The employee information in a company is stored in the relation
Employee (name, sex, salary, deptName)
Write an SQL query to find the names of departments where the average salary of male
employees is higher than the overall average salary in the company?
Question 6: Consider the tables A, B and C. How many tuples does the result of the
following SQL query contains?
Table A: Table B:

Id Name Age Id Name Age


12 Arun 60 15 Shreya 24
15 Shreya 24 25 Hari 40
98 Rohit 20
99 Rohit 11
99 Rohit 11

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:

Suppliers(sid:integer, sname:string, city:string, street:string)


Parts(pid:integer, pname:string, color:string)
Catalog(sid:integer, pid:integer, cost:real)

Consider the following relational query on the above database:

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

You might also like