SQL · Databases
43 questions · asked in 23 of 27 years (2000–2026)
MCQ 34 · MSQ 1 · NAT 81-mark 26 · 2-mark 17
2000Questions per year2026
Questions 16–20 of 43
Given the following schema:
employees(emp-id, first-name, last-name, hire-date, dept-id, salary)
departments(dept-id, dept-name, manager-id, location-id)
You want to display the last names and hire dates of all latest hires in their respective departments in the location ID 1700. You issue the following query:
employees(emp-id, first-name, last-name, hire-date, dept-id, salary)
departments(dept-id, dept-name, manager-id, location-id)
You want to display the last names and hire dates of all latest hires in their respective departments in the location ID 1700. You issue the following query:
SQL>SELECT last-name, hire-date
FROM employees
WHERE (dept-id, hire-date) IN
(SELECT dept-id, MAx(hire-date)
FROM employees JOIN departments USING(dept-id)
WHERE location-id = 1700
GROUP BY dept-id); What is the outcome?Answer: (B)
For each department at location 1700, the subquery gives the dept-id and the latest hire date. The outer query then keeps the employees whose (dept-id, hire-date) pair appears in that list. A multi-column IN is valid. GROUP BY can also be used in a subquery that has a join.
Answer: It executes and gives the correct result
Answer: It executes and gives the correct result
SQL allows duplicate tuples in relations, and correspondingly defines the multiplicity of tuples in the result of joins. Which one of the following queries always gives the same answer as the nested query shown below:
Select * from R where a in (select S. a from S)
Select * from R where a in (select S. a from S)
Answer: (C)
The nested query returns each tuple of R once for every copy of it in R, whether S has one matching tuple or many. It only tests if some tuple of S has the same a.
- (A) A plain join repeats an R tuple once for every matching S tuple, so extra duplicates appear.
- (B) DISTINCT also removes duplicates that are really present in R, so it can give fewer rows than the nested query.
- (C) S1 holds each value of a only once, so every R tuple matches at most one row of S1. The R tuples come out exactly as in the nested query, with R's own duplicates kept.
- (D) "is unique R" is not valid SQL syntax.
Answer: (C)
- (A) A plain join repeats an R tuple once for every matching S tuple, so extra duplicates appear.
- (B) DISTINCT also removes duplicates that are really present in R, so it can give fewer rows than the nested query.
- (C) S1 holds each value of a only once, so every R tuple matches at most one row of S1. The R tuples come out exactly as in the nested query, with R's own duplicates kept.
- (D) "is unique R" is not valid SQL syntax.
Answer: (C)
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. What does the following query return?
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. What does the following query return?
SELECT empName
FROM employee E
WHERE NOT EXISTS (SELECT custId
FROM customer C
WHERE C. salesRepId = E. empId
AND C. rating < > 'GOOD')Answer: (D)
The inner query picks the customers of this employee whose rating is not 'GOOD'. With NOT EXISTS, the employee is returned only when no such customer is found.
Every employee has at least one customer. So this means all customers of that employee have the rating 'GOOD'.
Answer: Names of all the employees with all their customers having a 'GOOD' rating
Every employee has at least one customer. So this means all customers of that employee have the rating 'GOOD'.
Answer: Names of all the employees with all their customers having a 'GOOD' rating
Consider the following relational schema.
Students(rollno: integer, sname: string)
Courses(courseno: integer, cname: string)
Registration(rollno: integer, courseno: integer, percent: real)
Which of the following queries are equivalent to this query in English?
“Find the distinct names of all students who score more than 90% in the course numbered 107”
(I)
(III)
(IV)
Students(rollno: integer, sname: string)
Courses(courseno: integer, cname: string)
Registration(rollno: integer, courseno: integer, percent: real)
Which of the following queries are equivalent to this query in English?
“Find the distinct names of all students who score more than 90% in the course numbered 107”
(I)
SELECT DISTINCT S.sname
FROM Students as S, Registration as R
WHERE R.rollno=S.rollno AND R.courseno=107 AND R.percent >90
(II) (III)
(IV)
Answer: (A)
Check that each query joins Students and Registration on rollno and keeps courseno 107 with percent above 90, and that it returns names without duplicates.
- I: the SQL joins on R.rollno = S.rollno, with R.courseno = 107 and R.percent > 90. DISTINCT removes repeated names. It matches.
- II: Registration and Students have only rollno in common, so the natural join is on rollno. The selection keeps courseno = 107 and percent > 90, and the projection on sname removes duplicates, since relational algebra works with sets. It matches.
- III: tuple calculus. It needs a Students tuple S and a Registration tuple R with S.rollno = R.rollno, R.courseno = 107 and R.percent > 90, and it returns T.sname = S.sname. A calculus result is a set, so there are no duplicates. It matches.
- IV: domain calculus. It needs a Students tuple with roll number S_R and name S_N, and a Registration tuple with the same S_R, course 107 and percent R_P > 90, and it returns S_N. It matches.
All four describe the same result.
Answer: (A)
- I: the SQL joins on R.rollno = S.rollno, with R.courseno = 107 and R.percent > 90. DISTINCT removes repeated names. It matches.
- II: Registration and Students have only rollno in common, so the natural join is on rollno. The selection keeps courseno = 107 and percent > 90, and the projection on sname removes duplicates, since relational algebra works with sets. It matches.
- III: tuple calculus. It needs a Students tuple S and a Registration tuple R with S.rollno = R.rollno, R.courseno = 107 and R.percent > 90, and it returns T.sname = S.sname. A calculus result is a set, so there are no duplicates. It matches.
- IV: domain calculus. It needs a Students tuple with roll number S_R and name S_N, and a Registration tuple with the same S_R, course 107 and percent R_P > 90, and it returns S_N. It matches.
All four describe the same result.
Answer: (A)
Consider the following relations A, B and C:
How many tuples does the result of the following SQL query contain?
How many tuples does the result of the following SQL query contain?
SELECT A.Id
FROM A
WHERE A.Age > ALL (SELECT B.Age
FROM B
WHERE B.Name = 'Arun') Answer: (B)
B has no 'Arun', so the subquery returns an empty set. The condition " ALL (empty set)" is true for every row.
So all 3 tuples of A are in the result.
Answer: 3
So all 3 tuples of A are in the result.
Answer: 3
Keep practising