Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Comph SQL (G9)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

SQL

Comprehensive
CST 308
Textbooks/Tutorials
To Refer
https://www.halvorsen.blog (Textbook)
https://cs.uwaterloo.ca (Notes)

2
Q1)Consider the following relation

Cinema (theater, address, capacity)

Which of the following options will be needed at the end of the SQL query
SELECT P1.address FROM Cinema P1 such that it always finds the addresses of theaters of
theaters with maximum capacity?

(a) WHERE P1.capacity > = All (select P2. capacity from Cinema P2)
(b) WHERE P1.capacity > = Any (select P2. capacity from Cinema P2)
(c) WHERE P1.capacity > All (select max (P2. capacity) from Cinema P2)
(d) WHERE P1.capacity >Any (select max (P2. capacity) from Cinema P2)

3
Ans:
option (a)
Explanation:
When the ALL condition is followed by a list, the optimizer expands the initial condition to all elements
of the list and strings them together with AND operators.
When the ANY condition is followed by a list, the optimizer expands the initial condition to all
elements of the list and strings them together with OR operators.
Option (b)returns address of all theatres except the one with minimum capacity
Option (c) returns 0 rows, since a theatre with a capacity greater than maximum capacity will not
exist.
Option (d) also returns null for the same reason as above.
Option (a) is the right answer, query will return addresses of theaters with maximum capacity.
Q2)Select operation in SQL is equivalent to

(a) the selection operation in relational algebra


(b) the selection operation in relational algebra, except that select in SQL retains duplicates
(c) the projection operation in relational algebra
(d) the projection operation in relational algebra, except that select in SQL retains duplicates

5
Ans: option (d)
Explanation:
Select operation in SQL returns a set of records containing duplicates. To eliminate duplicates,
DISTINCT keyword is specified.
Project operation in relational algebra, returns result set without repetition.

6
 Q3) Consider the following relations:

Consider the following SQL query.


SELECT S. Student_Name, sum (P.Marks) FROM Student S, Performance P WHERE S. Roll_No =P.Roll_No GROUP BY
S.Student_Name
The number of rows that will be returned by the SQL query is _________.
Student

Roll_No Student_Name

1 Raj

2 Rohit

3 Raj
Performance
Roll_No Course Marks
1 Math 80
1 English 70
2 Math 75
3 English 80
2 Physics 65
3 Math 80

7
Ans: 2
Explanation: 

There are  2 student names. The query returns sum (P.Marks) for each student.
Hence the output is :
Thus 2 rows are returned.

Raj 310
Rohit 140

8
 Q4)A relational schema for a train reservation database is given below.
Passenger (pid, pname, age)
Reservation (pid, class, tid)
What pids are returned by the following SQL query for the above instance of the tables?
SELECT 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    (d) 1, 5


Table: Passenger
pid pname age
 0 Sachin 65
1 Rahul    66

2 Sourav   67

3 Anil     69
Pid(Table : Reservation) class tid
 0 AC 8200
1 AC 8201
2 SC 8201
5 AC 8203
1 SC 8204
3 AC 8202
9
Ans: option (c)
Explanation: 
The above query is an example of synchronized subquery or correlated subquery. A
correlated sub-query is a sub-query that uses values from the outer query. The sub-query is
evaluated once for each row processed by the outer query. 

In the above query the outer query is


SELECT pid FROM Reservation WHERE class ‘AC’ AND EXISTS 
And the subquery is,
SELECT * FROM Passenger WHERE age > 65 AND Passenger. pid = Reservation.pid
The correlated subquery is evaluated once for each row processed by the outer query. The
outer query selects rows with pids: 0, 1, 5, 3, from Reservation table. Out of these, the
subquery conditions are met only for 1 and 3.

10
Q5)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

11
Ans: option (a)
Explanation:
“SELECT P.pid FROM Parts P WHERE P.color<> ‘blue’” gives pid of parts, which are not blue.
Note: "<>" indicates "not equal to".
“SELECT C.sid FROM Catalog C WHERE C.pid NOT IN (SELECT P.pid FROM Parts P WHERE
P.color<> ‘blue’)” gives sid of all suppliers who have supplied blue parts.
The whole query finally retrieves the name (sname) of suppliers, who have supplied a non-blue
part.

12
Q6)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
Ans: option (a)

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(a) Titles of the four most
expensive books (b) Title of the fifth most inexpensive book
(c) Title of the fifth most expensive book
(d) Titles of the five most expensive books

Ans: option (d)


Explanation:
The outer query selects all titles from "book" table. For each title the inner query will be evaluated.

For every selected book, the subquery will return the count of books which are more expensive than the selected
book. T.price > B.price is evaluated for every B.price. 

For the 1st book (B.price = 65) the inner query gives count 6 (75,85,95,105,115,125). 
For the 2nd book (B.price = 75) the inner query gives count 5 (85,95,105,115,125). 
For the 3rd book (B.price = 85) the inner query gives count 4 (95,105,115,125). 
For the 4th book (B.price = 95) the inner query gives count 3 (105,115,125). 
For the 5th book (B.price = 105) the inner query gives count 2 (115,125). 
For the 6th book (B.price = 115) the inner query gives count 1 (125). 
For the 7th book (B.price = 125) the inner query gives count 0. 

Hence the entire query will list out the title of a book when the count is less than 5. Hence as shown above from 3rd
book onwards the titles will be listed. GHI, JKL, MNO, PQR, STU will be listed. 
14
Q7)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)


Which one of the following statements is correct?
(a) All queries return identical row sets for any database
(b) Query2 and Query4 return identical row sets for all databases but there exist databases for which Query1 and
Query2 return different row sets.
(c) There exist databases for which Query3 returns strictly fewer rows than Query2.
(d) There exist databases for which Query4 will encounter an integrity violation at runtime.

15
Ans: option (b)
Explanation: Query1 and Query3 will produce duplicate rows if same student has applied for another course also.
Table : Enrolled
Student
Course
Minu Query2 & Query4:
Mtech student
Tinu
Bsc Minu
Vinu Vinu
Btech Vinu
Vinu
Mtech
Table : Paid
Student student
Amount Minu
Minu Vinu
2000
Vinu
4000
The output of each query for the above tables are shown below.
Query1 & Query3:

16
Q8)Consider the relation account (customer, balance) where customer is a primary  key and there are no null values. We
would like to rank customers according to  decreasing balance. The customer with the largest balance gets rank 1. Ties are
 not broke but ranks are skipped: if exactly two customers have the largest  balance they each get rank 1 and rank 2 is not
assigned.
Query1:
select A.customer, count(B.customer)
from account A, account B
where A.balance <=B.balance
group by A.customer
Query2:
select A.customer, 1+count(B.customer)
from account A, account B
where A.balance < B.balance
group by A.customer
Consider these statements about Query1 and Query2.
1. Query1 will produce the same row set as Query2 for some but not all databases.
2. Both Query1 and Query2 are correct implementation of the specification
3. Query1 is a correct implementation of the specification but Query2 is not
4. Neither Query1 nor Query2 is a correct implementation of the specification
5. Assigning rank with a pure relational query takes less time than scanning in decreasing balance order assigning ranks using ODBC.
Which two of the above statements are correct?
(a) 2 and 5 
(b) 1 and 3 
(c) 1 and 4 
(d) 3 and 5

17
Ans: option (c)
Explanation:
Both queries will produce the same result set only when there are no duplicate
balances in the table. So statement 1 is correct.
Query1 & Query2 is not the correct implementation because: Assume that we have
a table with n customers having same balance.In that case Query1 will give rank
"n" to each customer. But according to the question the rank assigned should be
"1". And Query2 will return an empty result set.

18
 Q9)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?
(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

Ans: option (c)

19
 Q10) Given the following statements:S1: A foreign key declaration can always be replaced
by an equivalent check assertion in SQL.
S2: Given the table R(a,b,c) where a and b together form the primary key, the following is a
valid table definition.
CREATE TABLE S ( a INTEGER, d INTEGER, e INTEGER, PRIMARY KEY (d), FOREIGN
KEY (a) references R)
Which one of the following statements is CORRECT?

ANS: both S1 AND S2 are false.

20
 Q11)In a B-Tree, each node represents a disk block. Suppose one block holds 8192 bytes.
Each key uses 32 bytes. In a B-tree of order M there are M – 1 keys. Since each branch is
on another disk block. We assume a branch is of 4 bytes. The total memory requirement for
a non-leaf node is

a)32 M - 32
b)236 M - 32
c)336 M - 36
d)432 M – 36

21
 

Ans: option b
Data:
Block size = 8192 bytes = 213 bytes
Key size = 32 byte
Block pointer = 4 byte
Order of the b-tree = M
Number of keys = M – 1 
Formula:
In a B-Tree, the size of non-leaf node = M × (Block pointer) + (M – 1) × (key + Record
pointer)
Calculation:
Assuming Record pointer = 0  (since nothing is mentioned about it)
Total size = M(4) + (M - 1)(32) = 36 M - 32

22
Q12) Consider a B+ tree in which the search key is 12 bytes long, block size is 1024 bytes,
record pointer is 10 bytes long and block pointer is 8 bytes long. The maximum number of
keys that can be accommodated in each non-leaf node of the tree is ____________.

23
ANS:50
 

Order of an internal node (non – leaf node) of the tree is the maximum number of children it can have.

Data for B+ Tree:


Disk Block size = D=1024 byte
Block pointer = B = 8 byte
Key field = K = 12 byte.
Record or data pointer = R = 10 byte
Order = p
Formula:
p × B + (p – 1) (K) ≤ D
Calculation:
p × 8 + (p – 1) (12) ≤ 1024
8p + 12p ≤ 1024 + 12
20p ≤ 1036
p ≤ 51.8
∴ p = 51
Number of key = p – 1 = 51 - 1 = 50
24
 

Q13) A file is organized so that the ordering of data records is the same as or close to the
ordering of data entries in some index. Then that index is called

a)Dense
b)Sparse
c)Clustered
d)Unclustered

ANS:c)Clustered
A file is organized so that the ordering of data records is the same as or close to the
ordering of data entries in some index is called clustered index, hence table can have only
one clustered index.

25
Q14)Consider a database implemented using B+ tree for file indexing and installed on a disk drive with
block size of 4 KB. The size of search key is 12 bytes and the size of tree/disk pointer is 8 bytes. Assume
that the database has one million records. Also assume that no node of the B+ tree and no records are
present initially in main memory. Consider that each record fits into one disk block. The minimum number
of disk accesses required to retrieve any record in the database
is ______.
Level Nodes Keys Pointers
Ans: 4 1 1 204 205
Data:
Block size = 4 KB = 4096 B 204 205 × 20
2 205
Search key = 12 B × 205 5
Tree pointer = 8 B
Number of DB records = 106 205 × 20 204 × 20 205 × 20
3
Explanation: 5 5 × 205 5 × 205
Each record fits into one disk block hence, Database block factor = 1. Means 1 block access from the database.
Moving on to the B+ tree. Since it's a B+ tree, an internal node only has search key values and tree pointers. Let p be the
order of an internal node.
Suppose the order of this tree is p, therefore
P (8) + (p-1) (12) ≤ 4096
Therefore, p = 205.
 
Number of entries in level 3 = 8.5 × 106. Therefore to access one of the 106 DB records, this B+ tree needs not more than
3 levels.
Total accesses = 3 + 1 = 4.
26
Q15)Which one of the following statements is NOT correct about the B+ tree data structure
used for creating an index of a relational database table?
1B+ Tree is a height-balanced tree

a)Non-leaf nodes have pointers to data records


b)Key values in each node are kept in sorted order
c)Each leaf node has a pointer to the next leaf node

Ans:(B)
Properties of B+ trees:
B+ tree is height balance tree.
Non leaf node has pointer to a node (leaf or non-leaf) and not pointer to data record
Key value in each node is in sorted order.
Leaf node has pointer to next leaf node.

27
Thanks!
Any questions?
You can Contact us at:
▪ sreelakshmi.manoor@gmail.com
▪ manjums0206@gmail.com
▪ vijayvnd003@gmail.com
▪ lubaibhadi@gmail.com

28

You might also like