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 26–30 of 43
Consider the table employee(empId, name, department, salary) and the two queries Q1 ,Q2 below. Assuming that department 5 has more than one employee, and we want to find the employees who get higher salary than anyone in the department 5, which one of the statements is TRUE for any arbitrary employee table?
Q1 : Select e.empId
From employee e
Where not exists
(Select * From employee s where s.department = "5" and
s.salary >=e.salary)
Q2 : Select e.empId
From employee e
Where e.salary > Any
(Select distinct salary From employee s Where s.department ="5"Answer: (A)
The goal is the employees whose salary is higher than that of every employee in department 5.
Q1: NOT EXISTS (a department-5 employee s with s.salary >= e.salary) means no one in department 5 earns as much as e or more. That is the same as e.salary being greater than every department-5 salary. So Q1 is correct. It works for any table, and a department-5 employee is never returned because he or she is compared with himself or herself.
Q2: e.salary > ANY (department-5 salaries) is true when e.salary is greater than at least one department-5 salary, that is, greater than the lowest one. This is weaker than the goal.
Example: department 5 salaries 30, 50, 80, and other employees with salaries 90 and 10. Q1 returns only the one with 90. Q2 returns the ones with 90 and also those with 50 and 80 in department 5, which are above the lowest salary only. So Q1 and Q2 differ, and only Q1 is correct.
Answer: (A)
Q1: NOT EXISTS (a department-5 employee s with s.salary >= e.salary) means no one in department 5 earns as much as e or more. That is the same as e.salary being greater than every department-5 salary. So Q1 is correct. It works for any table, and a department-5 employee is never returned because he or she is compared with himself or herself.
Q2: e.salary > ANY (department-5 salaries) is true when e.salary is greater than at least one department-5 salary, that is, greater than the lowest one. This is weaker than the goal.
Example: department 5 salaries 30, 50, 80, and other employees with salaries 90 and 10. Q1 returns only the one with 90. Q2 returns the ones with 90 and also those with 50 and 80 in department 5, which are above the lowest salary only. So Q1 and Q2 differ, and only Q1 is correct.
Answer: (A)
Consider a database with three relation instances shown below. The primary keys for the Drivers and Cars relation are did and cid respectively and the records are stored in ascending order of these primary keys as given in the tables. No indexing is available in the database.
Let n be the number of comparisons performed when the above SQL query is optimally executed. If linear search is used to locate a tuple in a relation using primary key, then n lies in the range:
select D.dname
from Drivers D
where D.did in (
select R.did
from Cars C, Reserves R
where R.cid = C.cid and C.colour = 'red'
intersect
select R.did
from Cars C, Reserves R
where R.cid = C.cid and C.colour = 'green'
)Let n be the number of comparisons performed when the above SQL query is optimally executed. If linear search is used to locate a tuple in a relation using primary key, then n lies in the range:
Answer: (B)
Count comparisons for each part, using linear search and the best order.
Red cars. Scan Cars (4 tuples) to find the red cars: 102 and 104. Doing it per red car over all 10 Reserves tuples would cost 20 more, so instead scan Reserves once and compare each cid with the red car cids. Reserves cids in order are 101, 102, 103, 104, 102, 103, 104, 101, 102, 103.
- Try 102 first, then 104: a cid of 102 (3 tuples) takes 1 comparison, and the other 7 take 2. This is .
- Try 104 first, then 102: a cid of 104 (2 tuples) takes 1, and the other 8 take 2. This is .
With the 4 Cars comparisons, red costs 21 to 22. The dids found are 22, 31, 64.
Green car. Scan Cars (4) to find green car 103, then compare it with the cid of all 10 Reserves tuples (10). This is 14 comparisons. The dids found are 22, 31, 74.
Intersect. Look for each did of the red list in the green list: 22 is 1st (1 comparison), 31 is 2nd (2 comparisons), and 64 is not found after 3 comparisons. This is . The result is 22, 31.
Drivers. Find did 22 and did 31 by linear search in Drivers: 22 is the 1st tuple (1 comparison) and 31 is the 3rd tuple (3 comparisons). This is 4 comparisons.
Total . This lies in the range 44 to 48.
Answer: (B)
Red cars. Scan Cars (4 tuples) to find the red cars: 102 and 104. Doing it per red car over all 10 Reserves tuples would cost 20 more, so instead scan Reserves once and compare each cid with the red car cids. Reserves cids in order are 101, 102, 103, 104, 102, 103, 104, 101, 102, 103.
- Try 102 first, then 104: a cid of 102 (3 tuples) takes 1 comparison, and the other 7 take 2. This is .
- Try 104 first, then 102: a cid of 104 (2 tuples) takes 1, and the other 8 take 2. This is .
With the 4 Cars comparisons, red costs 21 to 22. The dids found are 22, 31, 64.
Green car. Scan Cars (4) to find green car 103, then compare it with the cid of all 10 Reserves tuples (10). This is 14 comparisons. The dids found are 22, 31, 74.
Intersect. Look for each did of the red list in the green list: 22 is 1st (1 comparison), 31 is 2nd (2 comparisons), and 64 is not found after 3 comparisons. This is . The result is 22, 31.
Drivers. Find did 22 and did 31 by linear search in Drivers: 22 is the 1st tuple (1 comparison) and 31 is the 3rd tuple (3 comparisons). This is 4 comparisons.
Total . This lies in the range 44 to 48.
Answer: (B)
Consider a database with three relation instances shown below. The primary keys for the Drivers and Cars relation are did and cid respectively and the records are stored in ascending order of these primary keys as given in the tables. No indexing is available in the database.
What is the output of the following SQL query?
What is the output of the following SQL query?
select D.dname
from Drivers D
where D.did in (
select R.did
from Cars C, Reserves R
where R.cid = C.cid and C.colour = 'red'
intersect
select R.did
from Cars C, Reserves R
where R.cid = C.cid and C.colour = 'green'
)Answer: (A)
Take the red cars first. The drivers who reserved them are
Now take the green cars. The drivers who reserved them are
The intersection of the red and green results is , and these drivers are Karthikeyan and Boris.
Answer: A
Now take the green cars. The drivers who reserved them are
The intersection of the red and green results is , and these drivers are Karthikeyan and Boris.
Answer: A
Consider the relation enrolled (student, course) in which (student, course) is the
primary key, and the relation paid (student, amount) where student is the primary
key. Assume no null values and no foreign keys or integrity constraints. Given
the following four queries:
Query 1: Select student from enrolled where student in (select student from paid)
Query 2: Select student from paid where student in (select student from enrolled)
Query 3: Select E.student from enrolled E, paid P where E.student = P.student
Query 4: Select student from paid where exists
(select * from enrolled where enrolled.student = paid.student)
Which one of the following statements is correct?
Query 1: Select student from enrolled where student in (select student from paid)
Query 2: Select student from paid where student in (select student from enrolled)
Query 3: Select E.student from enrolled E, paid P where E.student = P.student
Query 4: Select student from paid where exists
(select * from enrolled where enrolled.student = paid.student)
Which one of the following statements is correct?
Answer: (B)
In enrolled a student can appear many times (one row per course). In paid each student appears at most once, because student is its primary key.
Query 1 gives one row for every enrolled row whose student is in paid, so a student with two courses appears twice.
Query 3 joins enrolled and paid on student. Each enrolled row matches at most one paid row, so it returns the same rows as Query 1 (duplicates included).
Query 2 and Query 4 start from paid and keep a student when at least one enrolled row has that student. IN and EXISTS test the same condition, and paid has no repeated student, so both return each such student once. They are identical for every database.
Query 1 and Query 2 differ when a paid student has two or more courses: with enrolled = {(s, c1), (s, c2)} and paid = {s}, Query 1 gives s twice and Query 2 gives s once. So (B) is true and (A) is false.
(C) is false: Query 3 returns at least one row for every student that Query 2 returns, so it never returns fewer rows.
(D) is false: a SELECT only reads data, and there are no constraints, so it cannot violate integrity.
Answer: (B)
Query 1 gives one row for every enrolled row whose student is in paid, so a student with two courses appears twice.
Query 3 joins enrolled and paid on student. Each enrolled row matches at most one paid row, so it returns the same rows as Query 1 (duplicates included).
Query 2 and Query 4 start from paid and keep a student when at least one enrolled row has that student. IN and EXISTS test the same condition, and paid has no repeated student, so both return each such student once. They are identical for every database.
Query 1 and Query 2 differ when a paid student has two or more courses: with enrolled = {(s, c1), (s, c2)} and paid = {s}, Query 1 gives s twice and Query 2 gives s once. So (B) is true and (A) is false.
(C) is false: Query 3 returns at least one row for every student that Query 2 returns, so it never returns fewer rows.
(D) is false: a SELECT only reads data, and there are no constraints, so it cannot violate integrity.
Answer: (B)
Consider the relation account (customer, balance) where customer is a primary
key and there are no mall values. We would like to rank customers according
to decreasing balance. The customer with the largest balance gets rank 1. Ties
are not broken but ranks are skipped: if exactly two customers have the largest
balance they each get rank 1 and rank 2 is not assigned.
Consider these statements about Query 1 and Query 2.
1. Query 1will produce the same row set as Query 2 for some but not all databases
2. Both Query 1 Query 2 are correct implementations of the specification
3. Query 1 is a correct implementation of the specification but Query 2 is not
4. Neither query 1 nor Query 2 is a correct implementation of the specification
5. Assigning rank with a pure relational Query takes less time than scanning in decreasing balance order the assigning ranks using ODBC
Which two of the above statements are correct?
| Query1: | |
| Query2: | |
Consider these statements about Query 1 and Query 2.
1. Query 1will produce the same row set as Query 2 for some but not all databases
2. Both Query 1 Query 2 are correct implementations of the specification
3. Query 1 is a correct implementation of the specification but Query 2 is not
4. Neither query 1 nor Query 2 is a correct implementation of the specification
5. Assigning rank with a pure relational Query takes less time than scanning in decreasing balance order the assigning ranks using ODBC
Which two of the above statements are correct?
Answer: (C)
The correct rank of a customer is 1 + (number of customers with a strictly larger balance), so tied customers share a rank.
Query 1 counts customers B with . This count includes A itself and every tie. If two customers tie for the largest balance, each gets 2 instead of 1. So Query 1 is wrong.
Query 2 uses the right formula, . But a customer with the largest balance has no B with a larger balance, so the join produces no row for it and that customer is missing from the output. So Query 2 is wrong too.
So statement 4 is true, and statements 2 and 3 are false.
Statement 1: on an empty relation both queries return the same (empty) result. On any non-empty relation Query 1 returns every customer (each A matches itself), while Query 2 leaves out the top-balance customers, so the results differ. So they agree for some but not all databases, and statement 1 is true.
Statement 5 is false: the pure relational query is a self-join that compares all pairs of rows, so it is not faster than sorting once and scanning in order.
Answer: (C)
Query 1 counts customers B with . This count includes A itself and every tie. If two customers tie for the largest balance, each gets 2 instead of 1. So Query 1 is wrong.
Query 2 uses the right formula, . But a customer with the largest balance has no B with a larger balance, so the join produces no row for it and that customer is missing from the output. So Query 2 is wrong too.
So statement 4 is true, and statements 2 and 3 are false.
Statement 1: on an empty relation both queries return the same (empty) result. On any non-empty relation Query 1 returns every customer (each A matches itself), while Query 2 leaves out the top-balance customers, so the results differ. So they agree for some but not all databases, and statement 1 is true.
Statement 5 is false: the pure relational query is a self-join that compares all pairs of rows, so it is not faster than sorting once and scanning in order.
Answer: (C)
Keep practising