DB 24-25-I MS Key
DB 24-25-I MS Key
Instructions:
1. This question paper contains 2 pages (4 sides of paper). Please verify.
2. Write your name, roll number, department above in block letters neatly with ink.
3. Write your final answers neatly with a blue/black pen. Pencil marks may get smudged.
4. Don’t overwrite/scratch answers especially in MCQ – ambiguous cases may get 0 marks.
5. Hardcoding attempts will not get any credit.
6. Be extremely precise in your answers and be careful not to make spelling or punctuation mistakes. We may
type your answers as SQLite queries to actual DB and give marks based on how correct the retrieved results are.
(DBs can do Math!) Deebo has an SQLite table mth with 4096 rows. The first column num mth
num fav
contains integers between 1 and 4096 (both included). Each number occurs exactly once –
1 0
no duplicates or missing numbers – but the numbers are not in sorted order. The second
1729 1
column fav has integers that are 0 or 1 indicating if that number is Deebo’s favourite or
42 1
not (1 = favourite, 0 = not). Note: SQLite supports modular arithmetic – if 𝑎, 𝑏 are integers, …
then the expression 𝑎 % 𝑏 (or mod(𝑎, 𝑏)) will give the remainder of 𝑎 when divided by 𝑏. 2607 0
Q1. Write an SQLite query to retrieve all even numbers from num sorted in ascending order. Your
result should have a single column. Using the mod operator will incur a 1 mark penalty.(3 marks)
Without Penalty (two options) With Penalty
SELECT m1.num SELECT 2 * num AS n
FROM mth AS m1, mth AS m2 FROM mth SELECT num
WHERE m1.num = 2*m2.num WHERE 2 * num IN ( FROM mth
ORDER BY m1.num ASC; SELECT num from mth WHERE num % 2 = 0
) ORDER BY num ASC;
ORDER BY n ASC;
Q2. Write a query to retrieve all primes from num sorted in descending order (2 is a prime 1 is not).
Your result should have a single column. Using mod operator will incur 1 mark penalty.(4 marks)
Without Penalty With Penalty
For Q4,5,6,7 it is implicit that the name of the (only) column of the views even and prime is
num. However, the use of any other name to refer to the column is allowed too. Also, no marks
would be deducted if the final response has column names other than 𝑛, 𝑝, 𝑞.
For Q4,5,6,7, no marks would be deducted for the use of % operator as the penalty was
mentioned only for Q1 and Q2.
Warning: joining views can be super slow if caching is not proper or if the system is running low
on memory or disk space. In such cases, converting the view to an actual table really helps.
CS315: Principles of Database Systems, IIT Kanpur Midsem (17 Sep 2024)
Name DEEBO 40 marks
Roll No 240007 Dept. AWSM Page 3 of 4
Q5. Create a view succ with 3 columns – the first with values 𝑛 from num in ascending order, the
second with the successor of 𝑛 if it exists in num and null otherwise and the third containing the
successor of 𝑛 if it exists in num and is also Deebo’s favourite and null otherwise. (6 marks)
WITH
fvrt AS ( SELECT num FROM mth WHERE fav = 1 ),
valid AS ( SELECT num from mth )
SELECT num AS n, num + 1 AS s, num + 1 AS f
FROM mth WHERE num + 1 IN fvrt
UNION
SELECT num AS n, num + 1 AS s, NULL AS f
FROM mth WHERE num + 1 IN valid AND num + 1 NOT IN fvrt
UNION
SELECT num AS n, NULL AS s, NULL AS f
FROM mth WHERE num + 1 NOT IN valid
ORDER BY n ASC;
Q6. Fill one box, give brief justification. Assume succ is a table, not a view. Deebo dislikes at least
the numbers 1 and 2607, maybe others too. PK ≡ PRIMARY KEY, U ≡ UNIQUE(3 x (1+1) = 6 marks)
Can the first column of the succ table become a PRIMARY KEY or satisfy UNIQUE constraint?
Only PK (not U) Give justification here
Only U (not PK) The first column is never NULL and takes unique values hence is eligible
Both PK and U to be both PK and U. Note that SQLite allows a column to be both PK
Neither PK nor U and U simultaneously (although such a specification is redundant).
Can the second column of the succ table become a PRIMARY KEY or satisfy UNIQUE constraint?
Only PK (not U) Give justification here
Only U (not PK) The second column will have a single NULL value so it cannot be PK but
Both PK and U can be U. Note that SQLite allows PK to be NULL in special circumstances
Neither PK nor U https://www.sqlite.org/lang_createtable.html#the_primary_key
Can the third column of the succ table become a PRIMARY KEY or satisfy UNIQUE constraint?
Only PK (not U) The third column will have multiple NULL values so it cannot be PK. It
Only U (not PK) shouldn’t have been U either but SQLite is weird: it considers NULLs to
Both PK and U be distinct. Thus, marks will be given for either option 2 or option 4.
Neither PK nor U https://www.sqlite.org/lang_createtable.html#unique_constraints
Page 4 of 4
Q7. Write an SQLite query to retrieve a bitmap index for primes. Your result should have 2 columns,
the first having values from num sorted in descending order and the second containing only 0 or 1
depending on whether the number is prime or not (prime ⇒ 1, not prime ⇒ 0). (5 marks)
SELECT num AS n, 0
FROM mth
WHERE num NOT IN prime
UNION
SELECT num AS n, 1
FROM mth
WHERE num IN prime
ORDER BY n DESC;
Q8. Dooba has written a relational expression to find Deebo’s favourite perfect squares from num
i.e. 𝑛 s.t. 𝑛 = 𝑚2 for some 𝑚 and 𝑛 is a favourite. ⋈ without a 𝜃 expression does a natural join.
Deebo suspects that Dooba’s expression will not give the output as intended. Help Deebo make all
corrections to the expression by filling the dashed boxes. Using your corrected expression, write
an SQLite query to retrieve all favourite perfect squares sorted in ascending order. (4+2=6 marks)
There may be other ways to correct the expression, but one set of corrections is the following:
1. Change the OR operator ∨ to an AND operator ∧ as we need favorite and perfect square
2. Change the second clause to M1.fav=1 since we need 𝑛 to be favorite and not 𝑚
3. Change the natural join ⋈ to a cross join × to allow all pairs to be compared
SELECT M1.num AS n
FROM mth AS M1, mth AS M2
WHERE M1.num = M2.num * M2.num AND M1.fav = 1
ORDER BY n ASC;
Doing an explicit CROSS JOIN will give the same result but may be slower at execution.