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 31–35 of 43
In an inventory management system implemented at a trading corporation, there are several tables designed to hold all the information. Amongst these, the following two tables hold information on which items are supplied by which suppliers, and which warehouse keeps which items along with the stock-level of these items.
Supply = (supplierid, itemcode)
Inventory = (itemcode, warehouse, stocklevel)
For a specific information required by the management, following SQL query has been written
For the warehouse at Nagpur, this query will find all suppliers wh
Supply = (supplierid, itemcode)
Inventory = (itemcode, warehouse, stocklevel)
For a specific information required by the management, following SQL query has been written
Select distinct STMP.supplierid
From Supply as STMP
Where not unique (Select ITMP.supplierid
From Inventory, Supply as ITMP
Where STMP.supplierid = ITMP.supplierid
And ITMP.itemcode = Inventory.itemcode
And Inventory.warehouse = 'Nagpur'); For the warehouse at Nagpur, this query will find all suppliers wh
Answer: (D)
NOT UNIQUE is true when the subquery result contains a repeated row. UNIQUE is true for an empty result and for a result with no repeated row.
For a supplier s in the outer query, the subquery returns s's supplierid once for each item that s supplies and that the Nagpur warehouse keeps (the item code matches an Inventory row with warehouse = 'Nagpur'). Taking (itemcode, warehouse) as identifying an Inventory row, each such item gives one row.
- 0 such items: empty result, UNIQUE is true, NOT UNIQUE is false, so s is not selected.
- exactly 1 such item: one row, no repeat, so s is not selected.
- 2 or more such items: the same supplierid repeats, NOT UNIQUE is true, so s is selected.
So the query finds suppliers that supply two or more items kept at Nagpur. (A), (B) and (C) are wrong because suppliers with no item or exactly one item are not selected.
Answer: (D)
For a supplier s in the outer query, the subquery returns s's supplierid once for each item that s supplies and that the Nagpur warehouse keeps (the item code matches an Inventory row with warehouse = 'Nagpur'). Taking (itemcode, warehouse) as identifying an Inventory row, each such item gives one row.
- 0 such items: empty result, UNIQUE is true, NOT UNIQUE is false, so s is not selected.
- exactly 1 such item: one row, no repeat, so s is not selected.
- 2 or more such items: the same supplierid repeats, NOT UNIQUE is true, so s is selected.
So the query finds suppliers that supply two or more items kept at Nagpur. (A), (B) and (C) are wrong because suppliers with no item or exactly one item are not selected.
Answer: (D)
A table 'student' with schema (roll, name, hostel, marks), and another table 'hobby' with schema (roll, hobbyname) contains records as shown below:
The following SQL query is executed on the above tables:
Relations S and H with the same schema as those of these two tables respectively contain the same information as tuples. A new relation S is obtained by the following relational algebra operation:
The difference between the number of rows output by the SQL statement and the number of tuples in S' is
The following SQL query is executed on the above tables:
select hostel
from student natural join hobby
where marks >= 75 and roll between 2000 and 3000; Relations S and H with the same schema as those of these two tables respectively contain the same information as tuples. A new relation S is obtained by the following relational algebra operation:
The difference between the number of rows output by the SQL statement and the number of tuples in S' is
Answer: (B)
SQL query. The natural join is on roll. It keeps students with marks 75 and roll between 2000 and 3000. These are 2369, 2581, 2643, 2872, 2926 and 2959. (2154 has 68 marks and 2711 has 72, so they are out.) Each student gives one row for each hobby:
2369 (hostel 7), 2581 (6), 2643 chess (5), 2643 hockey (5), 2872 (5), 2926 (5), 2959 (7).
Roll 2643 appears twice because it has two hobbies. The query selects hostel and has no DISTINCT, so it outputs 7 rows.
Relational algebra S'. The selection gives the same students (no student has marks exactly 75 or roll exactly 2000 or 3000, so and make no difference here). The projection on hostel in relational algebra works on sets, so it removes duplicates and leaves the hostels 5, 6 and 7. So S' has 3 tuples.
Difference .
Answer: (B)
2369 (hostel 7), 2581 (6), 2643 chess (5), 2643 hockey (5), 2872 (5), 2926 (5), 2959 (7).
Roll 2643 appears twice because it has two hobbies. The query selects hostel and has no DISTINCT, so it outputs 7 rows.
Relational algebra S'. The selection gives the same students (no student has marks exactly 75 or roll exactly 2000 or 3000, so and make no difference here). The projection on hostel in relational algebra works on sets, so it removes duplicates and leaves the hostels 5, 6 and 7. So S' has 3 tuples.
Difference .
Answer: (B)
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 Answer: (D)
For each book B, the subquery counts the books T that cost more than B. The condition keeps B when this count is less than 5, that is, when the count is 0, 1, 2, 3 or 4.
Prices are all different. The most expensive book has count 0, the second most expensive has count 1, and so on, up to the fifth most expensive with count 4. The sixth most expensive has count 5, so it is not kept. So the query lists the five most expensive books.
(A) is wrong because five books qualify, not four. (B) is about the cheap end, but the query counts books that cost more.
Answer: (D)
Prices are all different. The most expensive book has count 0, the second most expensive has count 1, and so on, up to the fifth most expensive with count 4. The sixth most expensive has count 5, so it is not kept. So the query lists the five most expensive books.
(A) is wrong because five books qualify, not four. (B) is about the cheap end, but the query counts books that cost more.
Answer: (D)
Consider two tables in a relational database with columns and rows as follows:
Roll_no is the primary key of the Student table, Dept_id is the primary key of the Department table and Student.Dept_id is a foreign key from Department.Dept_id
What will happen if we try to execute the following two SQL statements?
update Student set Dept_id = Null where Roll_on = 1
update Department set Dept_id = Null where Dept_id = 1
Roll_no is the primary key of the Student table, Dept_id is the primary key of the Department table and Student.Dept_id is a foreign key from Department.Dept_id
What will happen if we try to execute the following two SQL statements?
update Student set Dept_id = Null where Roll_on = 1
update Department set Dept_id = Null where Dept_id = 1
Answer: (C)
Statement (i): it sets Dept_id to NULL in the Student table (reading Roll_on as Roll_no). Here Dept_id is a foreign key, not the primary key, and a foreign key is allowed to be NULL. So (i) succeeds.
Statement (ii): it sets Dept_id to NULL in the Department table. Here Dept_id is the primary key, and a primary key can never be NULL. So (ii) fails. It would also leave student 2, who still refers to Dept_id 1, pointing at a department that no longer exists.
So (i) succeeds and (ii) fails.
Answer: (C)
Statement (ii): it sets Dept_id to NULL in the Department table. Here Dept_id is the primary key, and a primary key can never be NULL. So (ii) fails. It would also leave student 2, who still refers to Dept_id 1, pointing at a department that no longer exists.
So (i) succeeds and (ii) fails.
Answer: (C)
A table T1 in a relational database has the following rows and columns:
The following sequence of SQL statements was successfully executed on table T1.
The following sequence of SQL statements was successfully executed on table T1.
Update T1 set marks = marks + 5
Select avg(marks) from T1
What is the output of the select statement?
Answer: (C)
The update gives , so roll no. 4 stays NULL. The marks are now 15, 25, 35 and NULL.
avg ignores NULL values, so it averages only the three real values: .
Option (A) 18.75 is , which wrongly counts the NULL row as 0. The result is not NULL, because avg returns NULL only when every value is NULL.
Answer: (C)
avg ignores NULL values, so it averages only the three real values: .
Option (A) 18.75 is , which wrongly counts the NULL row as 0. The result is not NULL, because avg returns NULL only when every value is NULL.
Answer: (C)
Keep practising