DBMS MCQ
DBMS MCQ
DBMS MCQ
Explanation:
The six basic operators of relational algebra are the selection(σ ), the projection(π), the
Cartesian product (x) (also called the cross product or cross join), the set union (U), the set
difference (-), and the rename (p). These six operators are fundamental in the sense that
none of them can be omitted without losing expressive power. Many other operators have
been defined in terms of these six. Among the most important are set intersection, division,
and the natural join, but aggregation is not possible with these basic relational algebra
operations. So, we cannot run sum of all employees’ salaries with the six operations.
References:
http://en.wikipedia.org/wiki/Relational_algebra
http://faculty.ksu.edu.sa/zitouni/203%20Haseb%20%20Lecture%20Notes/Relional%20Algeb
ra.pdf
In the above question, Y uniquely determines X and Z, for a given value of Y you can easily
find out values of X and Z.
So, Y -> X and Y -> Z hold for above schema.
From rule of augmentation we can say YZ->X. If we understand the notion of FD, we don’t
need to apply axioms to find out which option is true, just by looking at the schema and
options we can say that (b) is true.
References:
http://www.cse.iitb.ac.in/~sudarsha/db-book/slide-dir/ch7.pdf
http://en.wikipedia.org/wiki/Functional_dependency
4. In SQL, relations can contain null values, and comparisons with null values are
treated as unknown. Suppose all comparisons with a null value are treated as false.
Which of the
following pairs is not equivalent? (GATE CS 2000)
(a) x = 5, not (not (x = 5)
(b) x = 5, x > 4 and x < 6, where x is an integer
(c) x < 5, not(x = 5)
(d) None of the above
Answer (c)
Explanation:
It doesn’t need much explanation. For all values smaller than 5, x < 5 will always be true but
x = 5 will be false.
In the above question R(A, B, C, D) is decomposed into R1 (A, B) and R2(C, D), and R1 ∩
R2 is empty. So, the decomposition is not lossless.
SELECT Count(*)
FROM temp
GROUP BY name;
Output:
count(*)
--------
2
1
1
Alternative way –
Statement (P) “An SQL query can contain a HAVING clause even if it does not have a
GROUP BY clause” is correct because Having caluse is applied after the aggregation phase
and must be used if you want to filter aggregate results and Having doesn’t require Group By
clause. A HAVING clause without a GROUP BY clause is valid and (arguably) useful syntax
in Standard SQL. Consider this example, which is valid Standard SQL:
Statement (S) "Not all attributes used in the GROUP BY clause need to appear in the
SELECT clause" is correct but if we use Group By clause must, there are limitations on what
we can put into the Select clause.
2) Given the basic ER and relational models, which of the following is INCORRECT?
(A) An attributes of an entity can have more that one value
(B) An attribute of an entity can be composite
(C) In a row of a relational table, an attribute can have more than one value
(D) In a row of a relational table, an attribute can have exactly one value or a NULL value
Answer (C)
The term 'entity' belongs to ER model and the term 'relational table' belongs to relational
model.
A and B both are true. ER model supports both multivalued and composite attributes
See this for more details.
(C) is false and (D) is true. In Relation model, an entry in relational table can can have
exactly one value or a NULL.
3) Suppose (A, B) and (C,D) are two relation schemas. Let r1 and r2 be the
corresponding relation instances. B is a foreign key that refers to C in r2. If data in r1
and r2 satisfy referential integrity constraints, which of the following is ALWAYS
TRUE?
Answer (A)
B is a foreign key in r1 that refers to C in r2. r1 and r2 satisfy referential integrity constraints.
So every value that exists in column B of r1 must also exist in column C of r2.
1) Consider the following transactions with data items P and Q initialized to zero:
T1: read (P) ;
read (Q) ;
if P = 0 then Q : = Q + 1 ;
write (Q) ;
T2: read (Q) ;
read (P) ;
if Q = 0 then P : = P + 1 ;
write (P) ;
2) Consider the following relations A, B, C. How many tuples does the result of the
following relational algebra expression contain? Assume that the schema of A U B is
the same as that of A.
Table A
Id Name Age
----------------
12 Arun 60
15 Shreya 24
99 Rohit 11
Table B
Id Name Age
----------------
15 Shreya 24
25 Hari 40
98 Rohit 20
99 Rohit 11
Table C
Id Phone Area
-----------------
10 2200 02
99 2100 01
(A) 7
(B) 4
(C) 5
(D) 9
Answer (A)
Id Name Age
----------------
12 Arun 60
15 Shreya 24
99 Rohit 11
25 Hari 40
98 Rohit 20
3) Consider the above tables A, B and C. How many tuples does the result of the
following SQL query contains?
SELECT A.id
FROM A
WHERE A.age > ALL (SELECT B.age
FROM B
WHERE B. name = "arun")
(A) 4
(B) 3
(C) 0
(D) 1
Answer (B)
The meaning of “ALL” is the A.Age should be greater than all the values returned by the
subquery. There is no entry with name “arun” in table B. So the subquery will return NULL. If
a subquery returns NULL, then the condition becomes true for all rows of A (See this for
details). So all rows of table A are selected.
1. Consider a relational table with a single record for each registered student with the
following attributes.
1. Registration_Number:< Unique registration number for each registered student 2.
UID: Unique Identity number, unique at the national level for each citizen 3.
BankAccount_Number: Unique account number at the bank. A student can have
multiple accounts or joint accounts. This attributes stores the primary account
number 4. Name: Name of the Student 5. Hostel_Room: Room number of the hostel
Which of the following options is INCORRECT?
(A) BankAccount_Number is a candidate key
(B) Registration_Number can be a primary key
(C) UID is a candidate key if all students are from the same country
(D) If S is a superkey such that S ∩ UID is NULL then S ∪ UID is also a superkey
Answer (A)
A Candidate Key value must uniquely identify the corresponding row in table.
BankAccount_Number is not a candidate key. As per the question “A student can have
multiple accounts or joint accounts. This attributes stores the primary account number”. If
two students have a joint account and if the joint account is their primary account, then
BankAccount_Number value cannot uniquely identify a row.
2) Consider a relational table r with sufficient number of records, having attributes A1,
A2,…, An and let 1 <= p <= n. Two queries Q1 and Q2 are given below.
The database can be configured to do ordered indexing on Ap or hashing on Ap.
Which of the following statements is TRUE?
(A) Ordered indexing will always outperform hashing for both queries
(B) Hashing will always outperform ordered indexing for both queries
(C) Hashing will outperform ordered indexing on Q1, but not on Q2
(D) Hashing will outperform ordered indexing on Q2, but not on Q1.
Answer (C)
If record are accessed for a particular value from table, hashing will do better. If records are
accessed in a range of values, ordered indexing will perform better. See this for more
details.
(A) 3
(B) 9
(C) 5
(D) 6
Answer (C)
Following will be contents of temporary table S
Borrower Bank_Manager
--------------------------
Ramesh Sunderajan
Suresh Ramgqpal
Mahesh Sunderjan
Following will be contents of temporary table T
Bank_Manager Loan_Amount
---------------------------
Sunderajan 10000.00
Ramgopal 5000.00
Sunderjan 7000.00
Following will be the result of natural join of above two tables. The key thing to note is that
the natural join happens on column name with same name which is Bank_Manager in the
above example. “Sunderjan” appears two times in Bank_Manager column, so their will be
four entries with Bank_Manager as “Sunderjan”.
4) Consider a database table T containing two columns X and Y each of type integer.
After the creation of the table, one record (X=1, Y=1) is inserted in the table.
Let MX and My denote the respective maximum values of X and Y among all records
in the table at any point in time. Using MX and MY, new records are inserted in the
table 128 times with X and Y values being MX+1, 2*MY+1 respectively. It may be noted
that each time after the insertion, values of MX and MY change. What will be the
output of the following SQL query after the steps mentioned above are carried out?
SELECT Y FROM T WHERE X=7;
(A) 127
(B) 255
(C) 129
(D) 257
Answer (A)
X Y
-------
1 1
2 3
3 7
4 15
5 31
6 63
7 127
......
......
Table : Reservation
pid class tid
---------------
0 AC 8200
1 AC 8201
2 SC 8201
5 AC 8203
1 SC 8204
3 AC 8202
What pids are returned by the following SQL query for the above instance of the
tables?
SLECT pid
FROM Reservation ,
WHERE class ‘AC’ AND
EXISTS (SELECT *
FROM Passenger
WHERE age > 65 AND
Passenger. pid = Reservation.pid)
(A) 1, 0
(B) 1, 2
(C) 1, 3
(S) 1, 5
Answer (C)
When a subquery uses values from outer query, the subquery is called correlated subquery.
The correlated subquery is evaluated once for each row processed by the outer query.
The outer query selects 4 entries (with pids as 0, 1, 5, 3) from Reservation table. Out of
these selected entries, the subquery returns Non-Null values only for 1 and 3.
4) Which of the following functional dependencies hold for relations R(A, B, C) and
S(B, D, E):
B → A,
A→C
The relation R contains 200 tuples and the rel ation S contains 100 tuples. What is the
maximum number of tuples possible in the natural join R◊◊S (R natural join S)
(A) 100
(B) 200
(D) 300
(D) 2000
Answer (A)
From the given set of functional dependencies, it can be observed that B is a candidate key
of R. So all 200 values of B must be unique in R. There is no functional dependency given
for S. To get the maximum number of tuples in output, there can be two possibilities for S.
1) All 100 values of B in S are same and there is an entry in R that matches with this value.
In this case, we get 100 tuples in output.
2) All 100 values of B in S are different and these values are present in R also. In this case
also, we get 100 tuples.
1) Consider two transactions T1 and T2, and four schedules S1, S2, S3, S4 of T1 and
T2 as given below:
T1 = R1[X] W1[X] W1[Y]
T2 = R2[X] R2[Y] W2[Y]
S1 = R1[X] R2[X] R2[Y] W1[X] W1[Y] W2[Y]
S2 = R1[X] R2[X] R2[Y] W1[X] W2[Y] W1[Y]
S3 = R1[X] W1[X] R2[X] W1[Y] R2[Y] W2[Y]
S1 = R1[X] R2[Y]R2[X]W1[X] W1[Y] W2[Y]
Which of the above schedules are conflict-serializable?
(A) S1 and S2
(B) S2 and S3
(C) S3 only
(D) S4 only
Answer (B)
There can be two possible serial schedules T1 T2 and T2 T1. The serial schedule T1 T2 has
the following sequence of operations
R1[X] W1[X] W1[Y] R2[X] R2[Y] W2[Y]
And the schedule T2 T1 has the following sequence of operations.
R2[X] R2[Y] W2[Y] R1[X] W1[X] W1[Y]
The Schedule S2 is conflict-equivalent to T2 T1 and S3 is conflict-equivalent to T1 T2.
2) Let R and S be relational schemes such that R={a,b,c} and S={c}. Now consider
the following queries on the database:
Answer (A)
I and II describe the division operator in Relational Algebra and Tuple Relational
Calculus respectively. See Page 3 of thisand slide numbers 9,10 of this for more details.
3) 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. Which one
of the following is the correct interpretation of the above query?
(A) Find the names of all suppliers who have supplied a non-blue part.
(B) Find the names of all suppliers who have not supplied a non-blue part.
(C) Find the names of all suppliers who have supplied only blue parts.
(D) Find the names of all suppliers who have not supplied only blue parts.
Answer (A)
The subquery “SELECT P.pid FROM Parts P WHERE P.color<> ‘blue’” gives pids of parts
which are not blue. The bigger subquery “SELECT C.sid FROM Catalog C WHERE C.pid
NOT IN (SELECT P.pid FROM Parts P WHERE P.color<> ‘blue’)” gives sids of all those
suppliers who have supplied blue parts. The complete query gives the names of all suppliers
who have supplied a non-blue part
4) Assume that, in the suppliers relation above, each supplier and each street within a
city has a unique name, and (sname, city) forms a candidate key. No other functional
dependencies are implied other than those implied by primary and candidate keys.
Which one of the following is TRUE about the above schema?
(A) The schema is in BCNF
(B) The schema is in 3NF but not in BCNF
(C) The schema is in 2NF but not in 3NF
(D) The schema is not in 2NF
Answer (A)
A relation is in BCNF if for every one of its dependencies X ? Y, at least one of the following
conditions hold:
X ? Y is a trivial functional dependency (Y ? X)
X is a superkey for schema R
Since (sname, city) forms a candidate key, there is no non-tirvial dependency X ? Y where X
is not a superkey
1) Let R and S be two relations with the following schema
R (P,Q,R1,R2,R3)
S (P,Q,S1,S2)
Where {P, Q} is the key for both schemas. Which of the following queries are
equivalent?
Answer (B)
See http://geeksquiz.com/gate-gate-cs-2008-question-82/ for explanation.
3) Which of the following is a correct attribute set for one of the tables for the correct
answer to the above question?
(A) {M1, M2, M3, P1}
(B) {M1, P1, N1, N2}
(C) {M1, P1, N1}
(D) {M1, P1}
Answer (A)
4) Consider the following relational schemes for a library database:
Book (Title, Author, Catalog_no, Publisher, Year, Price)
Collection (Title, Author, Catalog_no)
with in the following functional dependencies:
Assume {Author, Title} is the key for both schemes. Which of the following
statements is true?
(A) Both Book and Collection are in BCNF
(B) Both Book and Collection are in 3NF only
(C) Book is in 2NF and Collection is in 3NF
(D) Both Book and Collection are in 2NF only
Answer (C)
Table Collection is in BCNF as there is only one functional dependency “Title Author –>
Catalog_no” and {Author, Title} is key for collection. Book is not in BCNF because
Catalog_no is not a key and there is a functional dependency “Catalog_no –> Title Author
Publisher Year”. Book is not in 3NF because non-prime attributes (Publisher Year) are
transitively dependent on key [Title, Author]. Book is in 2NF because every non-prime
attribute of the table is either dependent on the key [Title, Author], or on another non prime
attribute.
2) The following table has two attributes A and C where A is the primary key and C is
the foreign key referencing A with on-delete cascade.
A C
-----
2 4
3 4
4 3
5 2
7 2
9 5
6 4
The set of all tuples that must be additionally deleted to preserve referential integrity
when the tuple (2,4) is deleted is:
(a) (3,4) and (6,4)
(b) (5,2) and (7,2)
(c) (5,2), (7,2) and (9,5)
(d) (3,4), (4,3) and (6,4)
Answer (C)
When (2,4) is deleted. Since C is a foreign key referring A with delete on cascade, all entries
with value 2 in C must be deleted. So (5, 2) and (7, 2) are deleted. As a result of this 5 and 7
are deleted from A which causes (9, 5) to be deleted.
3) 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
1) Consider the following log sequence of two transactions on a bank account, with
initial balance 12000, that transfer 2000 to a mortgage payment and then apply a 5%
interest.
1. T1 start
2. T1 B old=12000 new=10000
3. T1 M old=0 new=2000
4. T1 commit
5. T2 start
6. T2 B old=10000 new=10500
7. T2 commit
Suppose the database system crashes just before log record 7 is written. When the
system is restarted, which one statement is true of the recovery procedure?
(A) We must redo log record 6 to set B to 10500
(B) We must undo log record 6 to set B to 10000 and then redo log records 2 and 3
(C) We need not redo log records 2 and 3 because transaction T1 has committed
(D) We can apply redo and undo operations in arbitrary order because they are idempotent.
Answer (B)
2) 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:
Query1: select student from enrolled where student in (select student from
paid)
Query2: select student from paid where student in (select student from
enrolled)
Query3: select E.student from enrolled E, paid P where E.student =
P.student
Query4: select student from paid where exists
(select * from enrolled where enrolled.student = paid.student)
Table enrolled
student course
----------------
abc c1
xyz c1
abc c2
pqr c1
Table paid
student amount
-----------------
abc 20000
xyz 10000
rst 10000
Output of Query 1
abc
abc
xyz
Output of Query 2
abc
xyz
Output of Query 3
abc
xyz
Output of Query 4
abc
xyz
Query 1 and Query 3 may return repetitive student values as “student” is not a key in relation
enrolled, however query 2 and query 4 always return same row sets.
So, option (B) is correct.
Table r
A B C D
---------------------------
1 10 100 1000
1 20 200 1000
1 20 200 1001
Table r1
A B C
------------------
1 10 100
1 20 200
Table r2
A D
-----------
1 1000
1 1001
enroll table
studid courseid
------------------
1 1
2 1
3 1
2 2
3 3
3 2
Result of step b
studid courseid
---------------------
2 1
2 2
2 3
3 1
3 2
3 3
Result of step c
studid courseid
-------------------
2 3
2) Consider the relation employee(name, sex, supervisorName) with name as the key.
supervisorName gives the name of the supervisor of the employee under
consideration. What does the following Tuple Relational Calculus query produce?
(A) Names of employees with a male supervisor.
(B) Names of employees with no immediate male subordinates.
(C) Names of employees with no immediate female subordinates.
(D) Names of employees with a female supervisor.
Answer (C)
The query selects all those employees whose immediate subordinate is “male”. In other
words, it selects names of employees with no immediate female subordinates
3) 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”)
5) Consider the following schedules involving two transactions. Which one of the
following statements is TRUE?
Schedule S1
T1 T2
---------------------
r1(X)
r1(Y)
r2(X)
r2(Y)
w2(Y)
w1(X)
The schedule is neither conflict equivalent to T1T2, nor T2T1.
Schedule S2
T1 T2
---------------------
r1(X)
r2(X)
r2(Y)
w2(Y)
r1(Y)
w1(X)
The schedule is conflict equivalent to T2T1.